john john john john - 5 months ago 26
Android Question

Enable GPS setting in Android

I have set up googleMaps in my Application and I want to create an alert box in case the user does not have his/her GPS enabled.

I have everything set up properly but I think I have problem with the following function:

private void launchGPSOptions() {
final ComponentName toLaunch = new ComponentName("com.android.settings","com.android.settings.SecuritySettings");
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(toLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 0);
}


The case which I encounter is that when I have not my GPS enabled and then I press the button for finding my Location instead of dropping me into the GPS settings, is dropping me in the Security settings. I think the fault is the second argument of ComponentName.

Can someone help me finding the right class name for the GPS ?

Answer

Create a common class and access this method, as you will require this method to call in multiple places.

public static void showGpsSettings(Context context){
        Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        context.startActivity(intent);
}