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

时间:2013-04-14 16:42:52

标签: android android-intent android-preferences

我想从Intent启动系统的位置设置。我以编程方式知道它就像这样

Intent viewIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(viewIntent);

但我需要从Preference的XML中执行此操作。我试试这个

<Preference
    android:title="@string/pref_title" >
    <intent android:action="android.settings.ACTION_LOCATION_SOURCE_SETTINGS" />
</Preference>

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

2 个答案:

答案 0 :(得分: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;
        }
    });

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

<Preference
    android:key="goToLocationSettings"
    android:title="@string/pref_title" />

答案 1 :(得分:0)

试试这段代码:

<PreferenceScreen
    android:key="key_location"
    android:summary="location settings"
    android:title="Open location settings">

    <intent android:action="android.settings.ACTION_LOCATION_SOURCE_SETTINGS"/>

</PreferenceScreen>