mardi 26 juillet 2016

Enable Mock Locations and addTestProvider crashes in Android Marshmallow (v6)

I'm trying to enable mock location in my code, but could not make it work. It bascially crashes the app.

Here is what I did:

-- (using Android studio) I replicated AndroidManifest.xml from normal location to androidTest and test, with the following line added

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

(btw, should I duplicate the whole AndroidManifest.xml under androidTest and test, or just have above line only?)

-- and my code crashes at the following place

 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, this code is inside a

public class OverlayShowingService extends Service { }

since I'm defining some overlay buttons here.

So, my questions are

  1. when I run it (by commenting out the part that crashes), I still could not find the app under Setting --> Developer Option --> Mock location app. It is jut not listed there.

    I build the app by doing Build -> Generate Signed APK -> and use build type = debug.

    What did I do wrong here ? I tried suggestions under Enable Mock Locations in Android Marshmallow, but does not work

  2. why mLocationManager.addTestProvider crashe by app ? Any suggestions ?

Aucun commentaire:

Enregistrer un commentaire