通过 preferences.xml 中的 Intent 打开应用程序设置
Open application settings via Intent from preferences.xml
我想通过单击首选项条目来打开应用程序设置。所以我在 preferences.xml
中添加了一个意图
<PreferenceScreen
android:key="DELETE_DATA"
android:title="@string/pref_delete_data">
<intent android:action="android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS"/>
</PreferenceScreen>
并且我在 AndroidManifest.xml
中添加了一个 Intent-filter
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
...
使用上面的代码,没有任何动作或错误。但我不知道为什么...
如果我删除 <category>
会出现错误,因此 intent 被触发。有什么想法吗?
设备:HTC One M8 Android 4.4.4
对于其他对 OP 如何使用 OnPreferenceClickListener 感兴趣的人,check out this answer from a similar question。但是要直接从意图启动 activity,这对我有用:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_title_category_name">
<PreferenceScreen
android:title="@string/pref_title_activity_name"
android:key="pref_launch_settings">
<intent
android:action="android.intent.action.VIEW"
android:data="<data>"
android:targetPackage="<package name>"
android:targetClass="<target class>" />
</PreferenceScreen>
</PreferenceCategory>
data = 完整包名加上 activity 名称。即,com.example.myapp.ActivityName
包名称 = 清单文件根目录中定义的包名称。即,com.example.myapp
targetClass = 完整包路径,即 com.example.myapp.ActivityName
请注意,我在 PreferenceScreen
中以这种方式使用 <intent>
标签,而不是 PreferenceCategory
。
希望对您有所帮助!
<Preference android:title="@string/pref_app_version"
android:summary="@string/pref_app_version_summary">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="YOUR APPLICATION ID"
android:targetClass="com.my.java.packagename.MyActivity"/>
</Preference>
只是我今天注意到的一件事。如果您使用不同的产品口味,请确保 targetpackage 是您的 applicationId,如 build.gradle.
中所定义
将 android:data 设置为您的包名称。将 com.myapp(下方)替换为在清单文件中找到的应用程序包名称。
<PreferenceScreen
android:title="@string/system_settings_preference_title" >
<intent android:action="android.settings.APPLICATION_DETAILS_SETTINGS"
android:data="package:com.myapp"/>
</PreferenceScreen>
我想通过单击首选项条目来打开应用程序设置。所以我在 preferences.xml
中添加了一个意图 <PreferenceScreen
android:key="DELETE_DATA"
android:title="@string/pref_delete_data">
<intent android:action="android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS"/>
</PreferenceScreen>
并且我在 AndroidManifest.xml
中添加了一个 Intent-filter <activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
...
使用上面的代码,没有任何动作或错误。但我不知道为什么...
如果我删除 <category>
会出现错误,因此 intent 被触发。有什么想法吗?
设备:HTC One M8 Android 4.4.4
对于其他对 OP 如何使用 OnPreferenceClickListener 感兴趣的人,check out this answer from a similar question。但是要直接从意图启动 activity,这对我有用:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_title_category_name">
<PreferenceScreen
android:title="@string/pref_title_activity_name"
android:key="pref_launch_settings">
<intent
android:action="android.intent.action.VIEW"
android:data="<data>"
android:targetPackage="<package name>"
android:targetClass="<target class>" />
</PreferenceScreen>
</PreferenceCategory>
data = 完整包名加上 activity 名称。即,com.example.myapp.ActivityName
包名称 = 清单文件根目录中定义的包名称。即,com.example.myapp
targetClass = 完整包路径,即 com.example.myapp.ActivityName
请注意,我在 PreferenceScreen
中以这种方式使用 <intent>
标签,而不是 PreferenceCategory
。
希望对您有所帮助!
<Preference android:title="@string/pref_app_version"
android:summary="@string/pref_app_version_summary">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="YOUR APPLICATION ID"
android:targetClass="com.my.java.packagename.MyActivity"/>
</Preference>
只是我今天注意到的一件事。如果您使用不同的产品口味,请确保 targetpackage 是您的 applicationId,如 build.gradle.
中所定义将 android:data 设置为您的包名称。将 com.myapp(下方)替换为在清单文件中找到的应用程序包名称。
<PreferenceScreen
android:title="@string/system_settings_preference_title" >
<intent android:action="android.settings.APPLICATION_DETAILS_SETTINGS"
android:data="package:com.myapp"/>
</PreferenceScreen>