gpt4 book ai didi

android - 从 "Choose Application"列表中隐藏 NFC 应用程序/通过外部 NFC Intent 禁用启动

转载 作者:搜寻专家 更新时间:2023-11-01 07:45:15 25 4
gpt4 key购买 nike

我目前正在为 Android 编写几个支持 NFC 的应用程序,我想知道如何阻止我的应用程序出现在“选择应用程序”列表中,每当从启动器或非 NFC 扫描标签时,该列表就会打开应用程序。我只希望我的应用能够在标签打开时读取标签。

我当前的 Intent 过滤器:

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />

nfc_tech_filter.xml:

<tech-list>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
</tech-list>

最佳答案

您目前在应用的 list 中有几个与 NFC 相关的 Intent 过滤器:

<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

通过将此类 Intent 过滤器添加到您应用的 list 中,您表明某些 Activity 应启动(或者如果为相同 Intent 注册了多个 Activity,则应在 Intent 选择器中作为选项显示)这些 NFC 标签发现事件。

因此,如果您不希望您的应用可以通过这些 Intent 启动,则需要从 AndroidManifest.xml删除这些 Intent 过滤器。

但是,当您的应用程序的 Activity 处于前台时,您也将失去监听这些 Intent 的能力。由于 NFC Intent 是 Activity Intent ,您无法通过动态注册的广播接收器接收它们(参见 RubbelDieKatz 的回答)。相反,Android API 提供了替代方法来捕获 NFC 相关事件,同时您的应用程序的 Activity 显示在前台(这还允许您的应用程序优先于其他应用程序,这对于基于 list 的 NFC Intent 过滤器是不可能的):

  1. 自 API 级别 10(基本上是从 Android NFC 开始)开始,Android 就采用了前台调度系统。您可以在 onResume() 上使用方法 NfcAdapter.enableForegroundDispatch() 注册您的前台 Activity 以接收 NFC 事件(不要忘记在您的 Activity 失去焦点时取消注册onPause():

    public void onResume() {
    super.onResume();
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    adapter.enableForegroundDispatch(this, pendingIntent, null, null);
    }

    public void onPause() {
    super.onPause();
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    adapter.disableForegroundDispatch(this);
    }

    您甚至可以通过向 enableForegroundDispatch() 提供可选的 tech-list 和 intent-filters 参数来注册以仅接收特定的 NFC 发现事件子集。

    一旦您向前台调度系统注册了您的 Activity ,您将通过 onNewIntent() 回调接收 NFC 事件:

    public void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    ...
    }
  2. 从 API 级别 19 开始,有新的 NFC 阅读器模式 API。您现在可以注册以接收定期回调(而不是通过有时会导致一些问题的 Intent 调度)。您可以使用 NfcAdpater.enableReaderMode() 方法执行此操作:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    adapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A, null);
    }

    除了(或代替)NfcAdapter.FLAG_READER_NFC_A,您还可以使用其他标志来设置阅读器模式以监听其他标签技术。

    然后您将在标签发现时收到回调(接收方需要实现回调接口(interface) NfcAdapter.ReaderCallback):

    public void onTagDiscovered(Tag tag) {
    ...
    }

另见 What's the difference between enableReaderMode and enableForegroundDispatch?以获得两个选项之间更详细的比较。

关于android - 从 "Choose Application"列表中隐藏 NFC 应用程序/通过外部 NFC Intent 禁用启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45934270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com