简体   繁体   English

intent.getAction() 在 NFC 标签扫描期间在 onNewIntent() 中是 null

[英]intent.getAction() is null inside onNewIntent() during NFC tag scan

This is my first time working with NFC Tags.这是我第一次使用 NFC 标签。 I have declared the NFC scan activity like this in Manifest:我在清单中声明了这样的 NFC 扫描活动:

    <activity
        android:name=".main.NFCScanActivity"
        android:theme="@android:style/Theme.Holo.Light"
        android:launchMode="singleTop"
        android:exported="true">
        <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" />
    </activity>

Here is the nfc_tech_filter.xml file:这是 nfc_tech_filter.xml 文件:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
    <tech>android.nfc.tech.IsoDep</tech>
    <tech>android.nfc.tech.NfcA</tech>
    <tech>android.nfc.tech.NfcB</tech>
    <tech>android.nfc.tech.NfcF</tech>
    <tech>android.nfc.tech.NfcV</tech>
    <tech>android.nfc.tech.Ndef</tech>
    <tech>android.nfc.tech.NdefFormatable</tech>
    <tech>android.nfc.tech.MifareClassic</tech>
    <tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>

Whenever I scan my tag, onNewIntent() is call but when I try to call intent.getAction() inside onNewIntent, the value is always null.每当我扫描我的标签时,都会调用 onNewIntent(),但是当我尝试在 onNewIntent 中调用 intent.getAction() 时,该值始终为 null。

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}
private void handleIntent(Intent intent) {
    String action = intent.getAction(); // action here is null
}

It is more normal to use one of the 2 Foreground NFC API's when you want to handle NFC's when your App is in the Foreground.当您的应用程序位于前台时,当您想处理 NFC 时,使用 2 个前台 NFC API 之一更为正常。 Usually the Manifest NFC entries are only used to start your App by NFC trigger.通常 Manifest NFC 条目仅用于通过 NFC 触发器启动您的应用程序。

enableReaderMode is the better and newer of the 2 Foreground NFC Api's or there is the older enableForegroundDispatch enableReaderMode是 2 Foreground NFC Api 中更好和更新的,或者是较旧的enableForegroundDispatch

An example of using enableReaderMode使用enableReaderMode的示例

If using manifest entries for NFC you should really process the return of getIntent of your Activity in onCreate to see if that is an Intent created from an NFC trigger.如果为 NFC 使用清单条目,您应该真正处理onCreateActivitygetIntent返回,以查看它是否是从 NFC 触发器创建的 Intent。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM