Skip to content Skip to sidebar Skip to footer

Android Mock Gps Provider

My code snippet is: mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if(mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) { mL

Solution 1:

From the Android docs on the location manager:

removeTestProvider() throws 
IllegalArgumentException    ifno provider with the given name exists

So, if you're testing on the emulator, the settings for GPS might have been reset by the reboot (try checking your permissions and DDMS to enable it again). If on the device, you must have disabled GPS (go to Settings and enable GPS).

EDIT: Found something relevant: here. Basically there's something going on that seems erratic in the emulator. From the comments on that thread, try using Criteria.ACCURACY_FINE instead of LocationManager.GPS_PROVIDER, like:

LocationManagerlocationManager= (LocationManager)context.getSystemService( Context.LOCATION_SERVICE );

    Criteriacriteria=newCriteria();
    criteria.setAccuracy( Criteria.ACCURACY_FINE );
    Stringprovider= locationManager.getBestProvider( criteria, true );

    if ( provider == null ) {
        Log.e( TAG, "No location provider found!" );
        return;
    }

Go through that thread for further information.

Solution 2:

I was having the same problem, as some devices nowadays do not have GPS. A good method is as mentioned by 'varevarao', but i also encountered problem if I use the criteria method. On some devices, it still returns a null at 'getBestProvider' (see code below).

Criteriacriteria=newCriteria();
    criteria.setAccuracy( Criteria.ACCURACY_FINE );
    Stringprovider= locationManager.getBestProvider( criteria, false );

    if ( provider == null ) {
        criteria.setAccuracy( Criteria.ACCURACY_COARSE );
        provider = locationManager.getBestProvider( criteria, true );
        Log.d( "Function", "No location provider found!" );
        return;
    }

I did this to solve it (see code below). Not sure whether it is the best method, but it currently works for me. It works by getting the list of 'location providers' available on the device, then grabs the first (should not matter, as i am just using it for mock location).

List<String> tmpProviders = locationManager.getAllProviders();

   if (!tmpProviders.isEmpty() && tmpProviders.toArray()[0] != null && !tmpProviders.toArray()[0].toString().isEmpty() ) {
      mocLocationProvider = tmpProviders.toArray()[0].toString();
  } else {// your error goes here }

Post a Comment for "Android Mock Gps Provider"