The main activity is destroyed the first time the app is installed and after that I've got a background service that runs whenever there is available internet.
The background service detects location (amongst other things) and since the app is supposed to remain on the phone for months (it only works when there is internet and not all the time or else that'd drain the battery even further) well since it runs for a long time, the user can disable gps and forget to turn it back on before he is connected to internet the next time which can cause the app to crash when the google API client requests location so for now, the background app automatically opens the settings interface when it detects that gps is off in order to remind the user to turn it on but this solution isn't that good since the interface appears out of nowhere and the user can get confused.
So I want to make a prompt/alert dialogue that tells the user "the app [appName] requires gsp please turn it on" with "yes" and "no" choices and when the user clicks "yes" then the settings interface is shown. Is this doable ? Again, I'm not looking for a solution with an activity as I already know how to do this (no transparent activity either just using the service)
Here's how I'm doing things so far:
//This part of the code is right before getting the current location
if (!isLocationEnabled(this)) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
}
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}