19

我想从Intent. 我知道以编程方式它是这样的

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

但我需要从Preference. 我试试这样

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

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

4

2 回答 2

41

您可以创建一个: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" />
于 2013-04-14T16:48:44.950 回答
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>
于 2016-01-13T23:16:29.913 回答