starting an activity from preferences.xml

2019-03-27 18:11发布

问题:

I'm trying to go to the settings screen found at -

android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS

From an entry in my preferences activity but am having no luck. At the moment, pressing the entry just refreshes the same screen as I was on.

My preferences.xml looks like this:

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

And my manifest entry looks like this:

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

What am I doing wrong?

logcat:

12-11 15:53:34.170: INFO/ActivityManager(173): Starting activity: Intent { act=android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS cmp=com.my.app/.Preferences }
12-11 15:53:34.400: INFO/ActivityManager(173): Displayed activity com.my.app/.Preferences: 229 ms (total 229 ms)

Manifest:

<?xml version="1.0" encoding="utf-8"?>

    <activity android:name=".ViewActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MyPageOneActivity">
    </activity>
    <activity android:name=".MyPageTwoActivity">
    </activity>
    <activity android:name=".MyPageThreeActivity">
    </activity>
    <activity android:name=".Preferences">
        <intent-filter>
            <action android:name="com.my.app.PREFERENCES" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
</manifest>

Preferences.java ( sorry about the lack of formatting):

  package com.my.app;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Preferences extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);
    }
}

and preferences.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference 
    android:title="Address 1"
    android:key="customURLOne" 
    android:summary="Enter a new address for 1">
</EditTextPreference>
<EditTextPreference 
    android:title="Address 2"
    android:key="customURLTwo" 
    android:summary="Enter a new address for 2">
</EditTextPreference>
<EditTextPreference 
    android:title="Address 3"
    android:key="customURLThree" 
    android:summary="Enter a new address for 3">
</EditTextPreference>
 <Preference android:title="@string/my_location_settings">
    <intent android:action="android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS">
    </intent>
 </Preference>

回答1:

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>


回答2:

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