I want to launch System's Location Settings from an Intent
. I know that programmatically it goes like this
Intent viewIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(viewIntent);
but I need to do it from the XML of a Preference
. I try like this
<Preference
android:title="@string/pref_title" >
<intent android:action="android.settings.ACTION_LOCATION_SOURCE_SETTINGS" />
</Preference>
but it does not work, I always get an ActivityNotFoundException
. How can I launch that System Location Settings from an XML Intent?
The preferences are saved by the android. content. SharedPreferences class to an XML file that contains pairs of key-value; the values can be booleans, floats, ints, longs or strings.
Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.
An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases: Starting an activity. An Activity represents a single screen in an app.
The preference option doesn't exist anymore. You will need to right click the res -> new -> Android resource file and choose the resource type as xml in the dropdown. Then you will manually need to add the layout for preference xml.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With