Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...