偏好项和明确的意图 [英] Preference items and explicit intents

查看:52
本文介绍了偏好项和明确的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从首选项中调用活动

I'm trying to call an activity from a Preference item

理想情况下,我只想在 xml 中为该首选项指定一个明确的意图

Ideally I'd like to simply specify an explicit intent in the xml for that Preference item

但是我的 google fu 已经抛弃了我,我只能找到隐式意图的例子,例如

but my google fu has deserted me and I can only find examples of implicit intents, e.g.

<Preference android:title="@string/prefs_web_page" >
    <intent android:action="android.intent.action.VIEW"
            android:data="http://www.example.com" />
</Preference>

我已经以编程方式在别处调用了我的活动,例如

I have already called my activity elesewhere programatically, e.g.

Intent intent = new Intent(this, FileChooserActivity.class);

但我想直接从 xml 调用它

but I want to call this direct from the xml

这是可能的,还是我找错了树?

is this possible or am I barking up the wrong tree?

推荐答案

您可以这样做:

将此添加到您的首选项 XML:

Add this to your Preference XML:

<Preference
  android:key="TODO"
  android:title="@string/TODO"
  android:summary="@string/TODO">
    <intent
      android:action="com.example.test.pref.action"/>
</Preference>

并将其添加到要从首选项打开的活动清单中:

And add this in your Manifest to Activity to be opened from Preferences:

<activity android:name=".todo"
  android:label="@string/todo">
  <intent-filter>
    <action android:name="com.example.test.pref.action" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

确保 com.example.test.pref.action 是唯一的!

注意:如果您需要打开一个网站或任何无法编辑其清单条目的活动,您需要使用 Intent 从您的活动 com.example.test.pref.action 以编程方式打开它.

Note: If you need to open a Website or any Activity whose Manifest entry you can't edit, you'll need to programatically open it from your Activity com.example.test.pref.action with an Intent.

这篇关于偏好项和明确的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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