android - 使用 DDMS 模拟器控件将欺骗位置发送到平板电脑 android 设备

标签 android android-emulator gps ddms

我想测试我在我的 Android 平板电脑上开发的 GPS 应用程序,所以我需要从使用 DDMS 加载它的 KML 文件向它发送欺骗位置。我想使用物理设备,而不是模拟器。

问题是当我在设备 View 中选择我的外部设备(正在运行的应用程序进程)时,DDMS 中的仿真器控制部分被禁用(变灰)。 Insted 如果我选择模拟器,则模拟器控件显示为已启用但不是物理设备。

我的应用程序有

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

我的物理平板电脑配置为:

  • USB 调试。
  • 允许模拟位置

我正在使用 SDK r20,我的平板电脑有 android 4.0.3。

有什么想法吗?

非常感谢

最佳答案

我已经拼凑出这个问题的解决方案。

  • 转到设置->应用程序->开发并选择“允许模拟位置”。

  • 为 AndroidManifest.xml 添加ACCESS_MOCK_LOCATION权限:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION">
    
  • 实现一个使用 LocationManager 的类.addTestProvider() 函数。这将向应用程序指示它应该使用文件中的数据来构造新的 Location 对象。

  • 然后可以使用 LocationManager.setTestProviderLocation() 函数创建新位置。

    // start using mock locations
    try {
       mockLocationCreator = new MockLocationCreator(this.getApplicationContext());
       try {
           mockLocationCreator.openLocationList();
    
           mockLocationThread = new Thread(mockLocationCreator);
           mockLocationThread.start();
    
           Toast.makeText(this.getApplicationContext(), 
                          "Mock locations are in use", 
                          Toast.LENGTH_LONG)
                .show();
    
       } catch (IOException e) {
           Toast.makeText(this.getApplicationContext(), 
                          "Error: Unable to open / read data file", 
                          Toast.LENGTH_LONG)
                .show();
           mockLocationCreator = null;
       }
    } catch(SecurityException e) {
       Toast.makeText(this.getApplicationContext(), 
                      "Error: Insufficient Privileges", 
                       Toast.LENGTH_LONG)
            .show();
       Log.e(TAG, "unable to use mock locations, insufficient privileges", e);
    }
    

注意:不可能将模拟位置从 DDMS->Emulator Control->Location Controls 发送到真实设备,无论设备或 list 权限如何,这是错误的建议 here .


来源:

Android mock location on device? - 有关 list 权限和使用 telnet 命令行、链接和代码 fragment 的替代解决方案的信息。

Using Mock Locations in Android - 更冗长,包含一些死链接。

LocationManager Documentation - 官方 Android 文档

关于android - 使用 DDMS 模拟器控件将欺骗位置发送到平板电脑 android 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11591333/

相关文章:

Android Studio 看不到 AVD

Android Studio AVD - 模拟器 : Process finished with exit code 1

android - 根据可用性在 GPS 和网络提供商之间切换

android - 如何更改 Material AlertDialog 和 ProgressDialog 的微调器、文本和按钮颜色

java - ListView 中的按钮每个项目布局

android - Android Studio 在线数据库

android - 使用虚拟场景在 Android 模拟器中扫描条码

javascript - 仅在子菜单存在时显示图标

java - 使用模拟位置测试应用程序时出现 Android 错误