You can open with
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
You can return by pressing back button on device.
Answer from kjurkovic on Stack OverflowYou can open with
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
You can return by pressing back button on device.
I used the code from the most upvoted answer:
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
It opens the device settings in the same window, thus got the users of my android application (finnmglas/Launcher) for android stuck in there.
The answer for 2020 and beyond (in Kotlin):
startActivity(Intent(Settings.ACTION_SETTINGS))
It works in my app, should also be working in yours without any unwanted consequences.
Videos
Use below code to check. If it is disabled, dialog box will be generated
public void statusCheck() {
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
Here is a simple way of programmatically enabling location like Maps app:
protected void enableLocationSettings() {
LocationRequest locationRequest = LocationRequest.create()
.setInterval(LOCATION_UPDATE_INTERVAL)
.setFastestInterval(LOCATION_UPDATE_FASTEST_INTERVAL)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
LocationServices
.getSettingsClient(this)
.checkLocationSettings(builder.build())
.addOnSuccessListener(this, (LocationSettingsResponse response) -> {
// startUpdatingLocation(...);
})
.addOnFailureListener(this, ex -> {
if (ex instanceof ResolvableApiException) {
// Location settings are NOT satisfied, but this can be fixed by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) ex;
resolvable.startResolutionForResult(TrackingListActivity.this, REQUEST_CODE_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
});
}
And onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (REQUEST_CODE_CHECK_SETTINGS == requestCode) {
if(Activity.RESULT_OK == resultCode){
//user clicked OK, you can startUpdatingLocation(...);
}else{
//user clicked cancel: informUserImportanceOfLocationAndPresentRequestAgain();
}
}
}
You can see the documentation here: https://developer.android.com/training/location/change-location-settings
You can create a: PreferenceActivity that will represent you preferences and then you can assign an onClick to your preference like this:
Preference goToLocationSettings = (Preference) findPreference("goToLocationSettings");
goToLocationSettings.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(viewIntent);
return true;
}
});
And you will need to assign a key to your preference in the xml file:
<Preference
android:key="goToLocationSettings"
android:title="@string/pref_title" />
Try this code:
<PreferenceScreen
android:key="key_location"
android:summary="location settings"
android:title="Open location settings">
<intent android:action="android.settings.ACTION_LOCATION_SOURCE_SETTINGS"/>
</PreferenceScreen>
You can simply start an activity with this action:
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
I use it inside a dialog:
public static void displayPromptForEnablingGPS(final Activity activity)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
final String message = "Do you want open GPS setting?";
builder.setMessage(message)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
activity.startActivity(new Intent(action));
d.dismiss();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
d.cancel();
}
});
builder.create().show();
}
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
The solution here is not to try to use an intent to launch the location settings in Android. The way Android works on Android API >= 30, is that you first need to request location permissions for coarse and fine location. After the user has allowed the permission, then make another location permission request, for background location. So:
Step 1:
val permissionsToRequest: Array<String> = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
launcher.launch(permissionsToRequest)
Step 2 (assuming user allows coarse/fine location permission in step 1):
val permissionsToRequest: String =
Manifest.permission.ACCESS_BACKGROUND_LOCATION
launcher.launch(permissionsToRequest)
However, instead of a dialog coming up for the "Allow all the time" location permission, Android navigates user to the location settings for the Android app.
Use this method so that a pop will automatic come for Allow All The Time..
private void getPermission()
{
progressBar.setVisibility(View.VISIBLE);
ActivityResultLauncher<String[]> locationPermissionRequest =
registerForActivityResult(new ActivityResultContracts
.RequestMultiplePermissions(), result -> {
Boolean fineLocationGranted = result.getOrDefault(
Manifest.permission.ACCESS_FINE_LOCATION, false);
Boolean coarseLocationGranted = result.getOrDefault(
Manifest.permission.ACCESS_COARSE_LOCATION, false);
if (fineLocationGranted != null && fineLocationGranted) {
// Call Your Method
} else if (coarseLocationGranted != null && coarseLocationGranted) {
// Only approximate location access granted.
// Call Your Method
} else {
// No location access granted.
}
}
);
locationPermissionRequest.launch(new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
});
}