从preferences XML文件启动位置设置的意图意图、位置、文件、preferences

2023-09-06 09:39:42 作者:嗜血皇者

我想从意图启动系统的位置设置。我知道,编程它是这样

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);

但我需要从 preference 的XML做到这一点。我试着像这样

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>

,但它不工作,我总是得到一个 ActivityNotFoundException 。我怎样才能启动,从一个XML意图系统位置设置?

but it does not work, I always get an ActivityNotFoundException. How can I launch that System Location Settings from an XML Intent?

推荐答案

您可以创建一个: preferenceActivity ,将重新present你preferences,然后你可以指定一个的onClick 您preference是这样的:

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;
        }
    });

和您将需要在XML文件中的一个关键分配给您的preference:

And you will need to assign a key to your preference in the xml file:

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