处理NFC意图,只有当preference设置 [英] Handle NFC intents only when preference is set

查看:158
本文介绍了处理NFC意图,只有当preference设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过几个不同的应用程序(如的 NFC TagInfo NFC TagInfo恩智浦),它有一个选项可以自动启动(或显示在选择程序列表中),当标签被触摸的仅在的该选项设置在preferences,否则好像意图过滤器被完全忽视了系统。

I've seen several different apps (like NFC TagInfo and NFC TagInfo by NXP) that have an option to autostart (or show up in the "select app" list) when a tag is touched only if that option is set in the preferences, and otherwise it seems like the intent filter is totally ignored by the system.

如何在Android应用程序这样做?

How can this be done in an Android application?

推荐答案

完整的答案是使用<一个href="http://developer.android.com/guide/topics/manifest/activity-alias-element.html"><$c$c><activity-alias>在应用程序的清单,像这样的:

Full answer is to use an <activity-alias> in the app's manifest, like this:

<activity-alias
  android:name=".ActivityAlias"
  android:targetActivity=".YourActualActivity"
  android:enabled="false" >
  <intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity-alias>

YourActualActivity 是要能够打开或关闭的意图过滤器的类。当你不想禁用完整的活动,您使用要启用(在这里它的默认禁用)。

YourActualActivity is the class for which you want to be able to turn on or off the intent filter. As you do not want to disable the complete activity, you use an activity-alias in which you put the intent filter that you want to enable (here it's disabled by default).

在你的preferenceActivity您设置了一个当特定的设置改变时调用监听器。然后,它确实是这样的:

In your PreferenceActivity you set up a listener that is called when the particular setting is changed. It then does something like this:

getPackageManager().setComponentEnabledSetting(
  new ComponentName("your.package.name", "your.package.name.ActivityAlias"),
  changedBooleanPreference ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED :
    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  PackageManager.DONT_KILL_APP);

下面 changedBoolean preference 是已更改的设置。

请注意,这可能需要一段时间程序包管理器所做的更改生效。这取决于Android版本时和出现这种情况(也许还对装置的CPU速度)的速度有多快。

Note that it may take some time before the package manager has made the change effective. It depends on the Android version when and how fast that happens (and perhaps also on the CPU speed of the device).

这篇关于处理NFC意图,只有当preference设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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