从首选项XML文件启动位置设置意图

Jag*_*ago 18 android android-preferences android-intent

我想从一个启动系统的位置设置Intent.我以编程方式知道它是这样的

Intent viewIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(viewIntent);
Run Code Online (Sandbox Code Playgroud)

但我需要从一个XML的XML中做到这一点Preference.我试着这样

<Preference
    android:title="@string/pref_title" >
    <intent android:action="android.settings.ACTION_LOCATION_SOURCE_SETTINGS" />
</Preference>
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我总是得到一个ActivityNotFoundException.如何从XML Intent启动系统位置设置?

Emi*_*Adz 36

你可以创建一个:PreferenceActivity代表你的偏好,然后你可以onClick像这样分配你的偏好:

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;
        }
    });
Run Code Online (Sandbox Code Playgroud)

您需要在xml文件中为您的首选项分配一个键:

<Preference
    android:key="goToLocationSettings"
    android:title="@string/pref_title" />
Run Code Online (Sandbox Code Playgroud)