簡體   English   中英

NFC 標簽讀取活動未收到意圖

[英]NFC Tag Reading Activity not receiving intent

我有一個閱讀 NFC A 型標簽(非 NDEF)的活動。 我在開發人員模式下使用 Android 手機運行 Android Studio。

該項目在打開 NFC 的開發人員 Android 手機上正確啟動應用程序。 當我在手機上點擊我的 NFC 非接觸式卡時,手機會檢測到 NFC 卡,但會顯示手機上安裝的其他 NFC 讀取器應用程序的選項列表,而不是將意圖傳遞給前台應用程序。

如何讓前台項目的應用程序接收 NFC 意圖而不是彈出建議列表?

這是我的AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="testtag">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".TapTagActivity">
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
        <activity android:name=".ManageTagActivity"></activity>
        <activity android:name=".EnquireTagActivity"></activity>
        <activity android:name=".SelectTagActionActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TestActivity" />
    </application>
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="10" />
    <uses-feature
        android:name="android.hardware.nfc"
        android:required="true" />
</manifest>

這是活動 Java class:

package testtag;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;

public class TapTagActivity extends AppCompatActivity {

    private static Class targetActivity = null;
    private NfcAdapter nfcAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tap_tag);

        System.out.println("Tap Tag window ready ...");

        NfcManager nfcManager = (NfcManager) getSystemService(Context.NFC_SERVICE);
//        nfcAdapter = nfcManager.getDefaultAdapter();
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);

        if (nfcManager != null) {
            System.out.println("NFC Manager ready ...");
        }

        if (nfcAdapter != null) {
            System.out.println("NFC Adapter ready ...");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent nfcPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        if (nfcAdapter != null) {
            nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, null, null);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (nfcAdapter != null) {
            try {
                nfcAdapter.disableForegroundDispatch(this);
            } catch (IllegalStateException ex) {
                Log.e("ATHTAG","Error disabling NFC foreground dispatch", ex);
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        System.out.println("Doing onNewIntent() ...");

        // Use NFC to read tag
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        if (tag != null) {
            System.out.println("New tag found !!!");
        } else {
            System.out.println("No new tag found !!!");
        }
    }

    public static void setTargetRedirectIntent(Class activity) {
        targetActivity = activity;
    }
}

控制台設法打印:

I/System.out: Tap Tag window ready ...
I/System.out: NFC Manager ready ...
    NFC Adapter ready ...

如何從前台讀取 NFC 卡而不顯示已安裝 NFC 讀取器應用程序的建議列表?

我發現我必須在onResume()中設置enableForegroundDispatch()參數,而不是留下 null 值。

工作代碼:

package testtag;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.os.Bundle;
import android.util.Log;

public class TapTagActivity extends AppCompatActivity {

    private static Class targetActivity = null;
    private NfcAdapter nfcAdapter = null;
    private IntentFilter[] intentFiltersArray = null;
    private String[][] techListsArray = null;
    private PendingIntent pendingIntent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tap_tag);

        System.out.println("Tap Tag window ready ...");

        NfcManager nfcManager = (NfcManager) getSystemService(Context.NFC_SERVICE);
//        nfcAdapter = nfcManager.getDefaultAdapter();
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);

        if (nfcManager != null) {
            System.out.println("NFC Manager ready ...");
        }

        if (nfcAdapter != null) {
            System.out.println("NFC Adapter ready ...");
        }

        pendingIntent = PendingIntent.getActivity(
                this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        intentFiltersArray = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED)};
        techListsArray = new String[][]{new String[]{NfcA.class.getName()}, new String[]{NfcB.class.getName()}, new String[]{IsoDep.class.getName()}};
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent nfcPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        if (nfcAdapter != null) {
            nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (nfcAdapter != null) {
            try {
                nfcAdapter.disableForegroundDispatch(this);
            } catch (IllegalStateException ex) {
                Log.e("ATHTAG", "Error disabling NFC foreground dispatch", ex);
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        System.out.println("Doing onNewIntent() ...");

        // Use NFC to read tag
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        if (tag != null) {
            System.out.println("New tag found !!!");
        } else {
            System.out.println("No new tag found !!!");
        }
    }

    public static void setTargetRedirectIntent(Class activity) {
        targetActivity = activity;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM