从preferences.xml开始一个活动

qub*_*ubz 7 android manifest preferences android-activity

我正在尝试进入设置屏幕 -

android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS
Run Code Online (Sandbox Code Playgroud)

从我的偏好活动中的条目,但我没有运气.此刻,按下该条目只会刷新我所在的屏幕.

我的preferences.xml看起来像这样:

<Preference
         android:title="@string/my_location_settings">
    <intent android:action="android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS">
    </intent>
 </Preference>
Run Code Online (Sandbox Code Playgroud)

而我的清单条目看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

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)
Run Code Online (Sandbox Code Playgroud)

表现:

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

    <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>
Run Code Online (Sandbox Code Playgroud)

Preferences.java(抱歉缺少格式化):

  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);
    }
}
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

Ebo*_*ike 10

好吧,我想我明白了 - 你可能不清楚意图过滤器是什么.

您的清单条目说:

<activity android:name=".Preferences">
Run Code Online (Sandbox Code Playgroud)

这是您的活动的定义,称为[您的包裹] .Preferences.

 <intent-filter>
    <action android:name="android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS" />
Run Code Online (Sandbox Code Playgroud)

只要有人以ACTION_LOCATION_SOURCE_SETTINGS作为动作名称启动意图,就会触发首选项...

        <category android:name="android.intent.category.DEFAULT" />
Run Code Online (Sandbox Code Playgroud)

这应该是该操作的默认选项.

    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

显然,您不希望为您的活动使用Android API操作名称(除非您尝试提供Android内置位置源活动的替代方案).为主要首选项屏幕使用不同的操作名称,最好是包含您的包名称的内容.

编辑:另外,尝试使用PreferenceScreen:

<PreferenceScreen android:title="@string/my_location_settings">
    <intent android:action="android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS">
    </intent>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)