从preferences.xml启动一个活动

2024-01-03

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

android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS

来自我的偏好活动的条目,但我没有运气。目前,按下该条目只会刷新与我所在的屏幕相同的屏幕。

我的偏好设置.xml 如下所示:

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

我的清单条目如下所示:

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

我究竟做错了什么?

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)

显现:

<?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>

首选项.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);
    }
}

和偏好设置.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>

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

您的清单条目显示:

<activity android:name=".Preferences">

这是名为 [your package].Preferences 的活动的定义。

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

每当有人以 ACTION_LOCATION_SOURCE_SETTINGS 作为操作名称启动意图时,都会触发首选项...

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

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

    </intent-filter>
</activity>

显然,您不想为您的 Activity 使用 Android API 操作名称(除非您尝试提供 Android 内置位置源 Activity 的替代方案)。在您的主首选项屏幕上使用不同的操作名称,最好是包含您的包名称的名称。

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

<PreferenceScreen android:title="@string/my_location_settings">
    <intent android:action="android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS">
    </intent>
</PreferenceScreen>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从preferences.xml启动一个活动 的相关文章

随机推荐