Mark Comix
Mark Comix

Reputation: 1898

Check Location Provider Status

I'm making a test project to learn how to use the Location in Android.

I want to use WIFI/3G as default provider, but if there are not working (Airplane mode set ON for example), I want to switch to GPS.

My idea is check the NETWORK_PROVIDER status. If it not available, check the GPS_PROVIDER status. If this also not available, not use location and show a message.

To do that, I need to check the current Provider Status, but I can't find the way. There is any way to do that?

I had try with isProviderEnabled, but that method only tell me if the Provider can or can't be used, but not if really enabled.

The ideal idea is to do that before call requestLocationUpdates

Thanks and sorry for my english

Upvotes: 2

Views: 1781

Answers (2)

John Drake
John Drake

Reputation: 1

I'm getting ready to set this up in one of my applications but I haven't tried it yet. If you register a LocationListener using a call to LocationManager::requestLocationUpdates(), the framework should call your LocationListener's onStatusChanged(String provider, int status, Bundle extras) and provide a status. The status is supposed to be either AVAILABLE, OUT_OF_SERVICE, or TEMPORARILY_UNAVAILABLE.

Theoretically, you should receive a call to the onStatusChanged with a status of either OUT_OF_SERVICE or TEMPORARILY_UNAVAILABLE when the user switches to Airplane Mode. Then you can switch to using a different provider. When Airplane Mode is switched off you should again expect to receive a call with a status of AVAILABLE and you can revert back to using the previous provider.

Location Listener Reference: https://developer.android.com/reference/android/location/LocationListener

Upvotes: 0

Yazan
Yazan

Reputation: 6082

use this code to check

 LocationManager mlocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(!mlocMan .isProviderEnabled(LocationManager.GPS_PROVIDER) && !mlocMan .isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

//both providers are disabled, show a message...
}

EDIT:

//--------------------------------------------------------
private static boolean isAirplaneModeOn(Context context) {

   return Settings.System.getInt(context.getContentResolver(),
           Settings.System.AIRPLANE_MODE_ON, 0) != 0;

}

public void onCreate(){
// .. some code

LocationManager mlocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

if(!isAirplaneModeOn(this)){//airplan is OFF, check Providers

if(mlocMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER){
//start listener to requestUpdates ...
}else{
//NETWORK_PROVIDER disabled, try GPS
if(mlocMan .isProviderEnabled(LocationManager.GPS_PROVIDER)){
//start listener to requestUpdates ...
}else{
//show message, no providers enabled.
}

}//network disabled

}else{//airplan mode is ON
//here you need to check GPS only, as netwrok is OFF for sure
if(mlocMan .isProviderEnabled(LocationManager.GPS_PROVIDER)){
//start listener to requestUpdates ...
}else{
//show message, no providers enabled.
}

}//airplan is ON

}//onCreate()

might have some syntax errors, but i think this logic is what u need.

Upvotes: 0

Related Questions