2016-07-27 134 views
0

我试图在代码中启用模拟位置,但无法使其工作。它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服务)

所以,我的问题是

  1. 当我运行(通过评论指出,崩溃的部分),我还是没找到设置 - >开发人员选项 - >模拟位置应用程序下的应用程序。这里没有列出。

    我通过构建 - >生成签名APK - >并使用build type = debug构建应用程序。

    我在这里做错了什么?我尝试了Enable Mock Locations in Android Marshmallow下的建议,但不起作用

  2. 为什么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> 

回答

0

你在你的清单中添加你的服务?您需要内<application.../>

我也有addTestProvider下,这条线在我以前的项目做到 mLocationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);

请记住,您还可以选择哪些应用程序,你会使用在开发人员设置模拟地点(约上方电话)

+0

是补充,服务于清单中声明。如上所述,我无法在模拟位置设置下找到该应用程序,所以我无法选择它。 –

+0

任何其他建议? addTestProvider仍然会崩溃应用程序。 –

+0

您确实也为gps添加了权限吗?精细和/或粗糙的位置和GPS提供商 –

0

例如HOW TO

final String PROVIDER_NAME_GPS = android.location.LocationManager.GPS_PROVIDER; 

public void enableProvider(android.content.Context context, String providerName, int status) { 
    android.location.LocationManager locationManager = getLocationManager(context); 
    locationManager.addTestProvider(providerName, false, false, false, false, true, true, true, android.location.Criteria.POWER_LOW, android.location.Criteria.ACCURACY_FINE); 
    locationManager.setTestProviderEnabled(providerName, true); 
    locationManager.setTestProviderStatus(providerName, status, null, System.currentTimeMillis()); 
} 

public void setLocation(android.content.Context context, String providerName, double latitude, double longitude, double altitude, float accuracy) { 
    android.location.LocationManager locationManager = getLocationManager(context); 
    android.location.Location loc = new android.location.Location(providerName); 
    loc.setTime(System.currentTimeMillis()); 
    loc.setAccuracy(accuracy); 
    loc.setAltitude(altitude); 
    loc.setSpeed(0); 
    loc.setElapsedRealtimeNanos(android.os.SystemClock.elapsedRealtimeNanos()); //System.nanoTime()) 
    loc.setLatitude(latitude); 
    loc.setLongitude(longitude); 
    locationManager.setTestProviderLocation(providerName, loc); 
} 

public static LocationManager getLocationManager(Context context) { 
    return (LocationManager)context.getSystemService("location"); 
} 

public void notifyForeground(Context context, SimpleLocation simpleLocation) { 
    // set new location 
    setLocation(context, PROVIDER_NAME_GPS, simpleLocation.getLongitude(), simpleLocation.getLongitude(), ...); 
    // notify via not manager we have new fix 
} 

public static boolean isMockLocationEnabled(Context context) { 
    ContentResolver contentResolver = context.getContentResolver(); 
    return Secure.getString(contentResolver, "mock_location").equals("0"); 
} 

private boolean _providerEnabled = false; 

/** 
* MAIN METHOD - ENABLE PROVIDER IF NOT ENABLED YET 
* PASS NEW FIX TO LOCATION MANAGER 
*/ 
protected boolean enableProviderAndNotify(android.content.Context context, SimpleLocation simpleLocation) 
    if (isMockLocationEnabled(context)) { 
     if (!_providerEnabled) { 
      enableProvider(context, PROVIDER_NAME_GPS, android.location.LocationProvider.AVAILABLE); 
      _providerEnabled = true; 
     } 
     notifyForeground(context, simpleLocation); 
     return true; 
    } else { 
     _providerEnabled = false; 
     disableAll(); 
     warnNoMockLocationEnabled(context); 
    } 
    return false; 
} 
  • 揭示应用程序中开发人员设定选择API模拟位置应用> M在清单

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