通过preferences.xml中的Intent打开应用程序设置 [英] Open application settings via Intent from preferences.xml

查看:109
本文介绍了通过preferences.xml中的Intent打开应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过单击首选项条目来打开应用程序设置.因此,我将一个意图添加到了preferences.xml

I would like to open the application settings by clicking on a preferences entry. So I added an intent to the preferences.xml

    <PreferenceScreen
        android:key="DELETE_DATA"
        android:title="@string/pref_delete_data">
        <intent android:action="android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS"/>
    </PreferenceScreen>

,并且我已将一个Intent过滤器添加到AndroidManifest.xml

and I've added an Intent-filter to the AndroidManifest.xml

    <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> ,则会出现错误,因此触发了意图.有什么想法吗?

With the code above, there is no action or error. But I don't know why... If I'm removing the <category> there comes an error, so the intent is fired. Any ideas?

设备:装有Android 4.4.4的HTC One M8

Device: HTC One M8 with Android 4.4.4

推荐答案

对于其他对OP如何使用OnPreferenceClickListener感兴趣的人,

For others interested in how OP used the OnPreferenceClickListener, check out this answer from a similar question. But to launch an activity directly from an intent, this worked for me:

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

数据 =完整的软件包名称加上活动名称.即com.example.myapp.ActivityName

data = Full package name plus activity name. I.e., com.example.myapp.ActivityName

软件包名称 =清单文件根目录中定义的软件包名称.即com.example.myapp

package name = Package name as defined in the root of your manifest file. I.e., com.example.myapp

targetClass =再次完整的软件包路径,即com.example.myapp.ActivityName

targetClass = full package path again, i.e., com.example.myapp.ActivityName

请注意,我在 PreferenceScreen 而不是 PreferenceCategory 内以这种方式使用< intent> 标记.

Note that I'm using the <intent> tag this way inside of a PreferenceScreen, rather than a PreferenceCategory.

希望有帮助!

这篇关于通过preferences.xml中的Intent打开应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆