简体   繁体   English

NFC - OnNewIntent() 有时无法在 android 设备上运行

[英]NFC - OnNewIntent() sometime not working on android device

I have been detecting NFC enabled card in my app to read tag value.我一直在我的应用程序中检测到启用 NFC 的卡来读取标签值。 Following is my code以下是我的代码

OnCreate() OnCreate()

 nfcAdapter = NfcAdapter.getDefaultAdapter(AuthDriverCard.this);
    piTap = PendingIntent.getActivity(
            this, TAP_REQUEST_CODE, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_UPDATE_CURRENT);
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    nfcIntentFilter = new IntentFilter[]{techDetected, tagDetected, ndefDetected};

OnResume恢复

 if (nfcAdapter != null) {
        nfcAdapter.enableForegroundDispatch(AuthDriverCard.this, piTap, nfcIntentFilter, null);
    }

OnPause暂停

 if (nfcAdapter != null) {
        nfcAdapter.enableForegroundDispatch(AuthDriverCard.this, piTap, nfcIntentFilter, null);
    }

OnNewIntent() OnNewIntent()

  protected void onNewIntent(Intent intent) {
    // my logic here
   }

This code works fine almost every time.这段代码几乎每次都能正常工作。 But some time card is not detecting and i found that OnNewIntent() is not firing .但是有些时间card is not detecting ,我发现OnNewIntent() is not firing

What could be the problem.可能是什么问题呢。 Do i need to set intent filter in manifest also?我还需要set intent filter in manifest吗? I haven't set it in manifest so far but it worked for me without any problem.到目前为止,我还没有在清单中设置它,但它对我有用,没有任何问题。 Is there any problem in my java code?我的 java 代码有问题吗?

Please suggest请建议

NOTE - Restart(after killing app) app solve the problem.注意- 重新启动(杀死应用程序后)应用程序解决问题。 card detection works after that.之后卡检测工作。

You need to add the following in intent-filter section of Manifest file, if you haven't added already:如果您尚未添加,则需要在 Manifest 文件的 intent-filter 部分添加以下内容:

<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>

Eg:例如:

<application
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".activities.LauncherActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <action android:name="android.nfc.action.TECH_DISCOVERED"/>

            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

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

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