Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch Location Settings intent from preferences XML file

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?

like image 582
Jago Avatar asked Apr 14 '13 16:04

Jago


People also ask

How are preferences saved to an XML file?

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.

How to enable location services in Android studio?

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.

What is intent file in Android?

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.

Where is the preference option in Android Studio?

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.


1 Answers

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" />
like image 105
Emil Adz Avatar answered Oct 23 '22 22:10

Emil Adz