2

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

Mark Comix
  • 1,878
  • 7
  • 28
  • 44

2 Answers2

1

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.

Yazan
  • 6,074
  • 1
  • 19
  • 33
  • That's doesn't do what I want. isProviderEnabled only tell you if the user let you use GPS_PROVIDER or NETWORK_PROVIDER. It doesn't tell you if the GPS it is turned ON or OFF or if the cellphone it is on airplane mode – Mark Comix May 06 '14 at 14:06
  • i can't see how this is not what you want! 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. and thats what i gave to you. – Yazan May 06 '14 at 14:22
  • tell you if the user let you use GPS_PROVIDER , taht means its ENABLED. – Yazan May 06 '14 at 14:23
  • Try this, turn the cellphone in Airplane mode and call locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER). It will return you True. For me, thats wrong. – Mark Comix May 06 '14 at 14:36
  • well, now i got you, check this, plus the code above you will get what you need http://stackoverflow.com/questions/4319212/how-can-one-detect-airplane-mode-on-android – Yazan May 06 '14 at 14:39
  • I just recheck for locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) and if the GPS it is turned off, it return false. So maybe it is a problem with NETWORK_PROVIDER. I'm lost! – Mark Comix May 06 '14 at 14:40
  • i think its bit tricky, i will post you full code, might help you – Yazan May 06 '14 at 14:50
  • first part solved my problem with locations (Android P) – Irodoku Aug 31 '19 at 01:03
0

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