starting an activity from preferences.xml

五迷三道 提交于 2019-12-03 14:02:54

Okay, I think I understand - you may be unclear about what an intent-filter is.

Your manifest entry says:

<activity android:name=".Preferences">

This is the definition for your activity called [your package].Preferences.

 <intent-filter>
    <action android:name="android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS" />

Preferences will be triggered whenever somebody starts an intent with ACTION_LOCATION_SOURCE_SETTINGS as the action name...

        <category android:name="android.intent.category.DEFAULT" />

This is supposed to be the default option for that action.

    </intent-filter>
</activity>

Obviously, you don't want to use an Android API action name for your activity (unless you're trying to provide an alternative to Android's built-in location source activity). Use a different action name for your main preferences screen, preferably something with your package name in it.

EDIT: Also, try using a PreferenceScreen:

<PreferenceScreen android:title="@string/my_location_settings">
    <intent android:action="android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS">
    </intent>
</PreferenceScreen>

Nothing works for me so i did: (I think it is a bad idea but...)

1.Remove this filter from manifest

<intent-filter>
<action android:name="android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

1. Make preference easier

<Preference android:key="simple_key"
        android:title="@string/title_simple_key">
    </Preference>

2. Add Clicklistener in your PreferenceFragment

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.layout.preferences);
    // Load the preferences from an XML resource
    findPreference("simple_key").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivity(new Intent(
                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            return false;
        }
    });

}

P.S. Sorry for my English

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!