2016-07-27 4 views
0

Ich versuche, in meinem Code Scheinposition zu aktivieren, konnte es aber nicht funktionieren. Es stürzt grundsätzlich die App ab. HierAktivieren Sie Mock-Standorte und fügen Sie TestProvider Abstürze in Android Marshmallow (v6)

ist das, was ich getan habe:

- (mit Android Studio) Ich AndroidManifest.xml von normaler Lage zu androidTest repliziert und Test mit der folgenden Zeile

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

hinzugefügt (btw, solltest ich duplizieren Sie die ganze AndroidManifest.xml unter androidTest und Test oder gerade über die Linie nur)

- und mein Code stürzt an folgenden Orten

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); 

} 

btw, ist dieser Code in einem

public class OverlayShowingService extends Service { } 

seit ich hier einige Overlay-Schaltflächen bin definieren. (Update: zur Klärung, erklärte ich den Dienst in manifest.xml)

Also, meine Fragen sind

  1. , wenn ich es laufen (durch den Teil Kommentierung aus, die abstürzt), konnte ich noch nicht gefunden die App unter Einstellungen -> Entwickler-Option -> Mock-Location-App. Es ist dort nicht aufgeführt.

    Ich baue die App, indem ich Build -> Signed APK -> generieren und Build-Typ = debug verwenden.

    Was habe ich hier falsch gemacht? Ich habe versucht, Vorschläge unter Enable Mock Locations in Android Marshmallow, aber funktioniert nicht

  2. Warum stürzt mlacationManager.addTestProvider? Irgendwelche Vorschläge ?

-Update: zur Klärung, hier ist das Manifest (für den Test):

<?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> 

Antwort

0

Haben Sie Ihren Dienst in Ihrem Manifest hinzufügen? Sie tun müssen, um so innerhalb <application.../>

Ich hatte auch diese Zeile unter addTestProvider in meinem vorherigen Projekt mLocationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);

Beachten Sie, dass Sie wählen auch die App, die Sie für simulierten Standort- unter Entwickler-Einstellungen verwenden werden (über etwa Telefon)

+0

Ja, wird der Dienst in Manifest erklärt.Wie bereits erwähnt, konnte ich die App nicht unter der Einstellung für den Scheinort finden. Daher kann ich sie nicht auswählen. –

+0

irgendeinen anderen Vorschlag? Der addTestProvider stürzt die App immer noch ab. –

+0

Sie haben auch die Berechtigungen für GPS richtig? Feine und/oder Grobe Standort und GPS-Anbieter –

0

Beispiel WIE:

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; 
} 
  • App offenbaren in Entwickler-Einstellungen Falschen Standort App für api wählen> M in Manifest hinzufügen

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

Verwandte Themen