我试图在代码中启用模拟位置,但无法使其工作。它bascially崩溃的应用程序。在Android Marshmallow中启用模拟位置和addTestProvider崩溃(v6)
这里是我做过什么:
- (使用的是Android工作室)我复制AndroidManifest.xml中从正常位置androidTest和测试,使用以下行添加
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
(顺便说一句,应该我复制了整个AndroidManifest.xml中androidTest和测试下,或者只是有上面一行只)
- ?和我的代码崩溃在以下地方
final String providerName = "MyWalkPokemonGPSProvider";
private LocationManager mLocationManager;
public void setMyGPS(){
mLocationManager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);
// this is where it crashes
// if it is commented out, then it runs (but still does not allow MOCK)
mLocationManager.addTestProvider(providerName, true, false, false, false, true, true, true,
Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
Location loc = new Location(providerName);
loc.setTime(System.currentTimeMillis());
loc.setLongitude(37.331784);
loc.setLatitude(-121.885547);
mLocationManager.setTestProviderLocation(providerName, loc);
new Location(LocationManager.GPS_PROVIDER);
}
顺便说一句,这个代码是内部的
public class OverlayShowingService extends Service { }
因为我在这里定义一些覆盖按钮。 (更新:澄清,我宣布的manifest.xml服务)
所以,我的问题是
当我运行(通过评论指出,崩溃的部分),我还是没找到设置 - >开发人员选项 - >模拟位置应用程序下的应用程序。这里没有列出。
我通过构建 - >生成签名APK - >并使用build type = debug构建应用程序。
我在这里做错了什么?我尝试了Enable Mock Locations in Android Marshmallow下的建议,但不起作用
为什么mLocationManager.addTestProvider崩溃?有什么建议么 ?
更新:澄清,这里是清单(用于测试):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.michaelandjewel.walkmypokemon">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.michaelandjewel.walkmypokemon.OverlayShowingService"
android:exported="false" />
</application>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
</manifest>
是补充,服务于清单中声明。如上所述,我无法在模拟位置设置下找到该应用程序,所以我无法选择它。 –
任何其他建议? addTestProvider仍然会崩溃应用程序。 –
您确实也为gps添加了权限吗?精细和/或粗糙的位置和GPS提供商 –