获取当前 activity 以处理发现的 NFC TAG

Get the current activity to handle NFC TAG discovered

我有一个包含多个活动的 android 应用程序。我希望当前 activity 处理 nfc 发​​现的事件,因为应用程序的状态决定了我想对标签做什么

如所附代码所示,我已经在每个 activity 上设置了意图,并在每个活动中实现了 onResume、onPause 和 onNewIntent 方法。

然而,由于某些原因,MainActivty 是唯一被调用的活动,即使其他活动之一是活动活动。例如。它是具有当前 GUI 的那个。

你们知道如何让主动 activity 处理发现的 NFC 吗?

非常感谢任何帮助:)

这是 ApplicationManifest

<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <activity
        android:name=".ConfigureStableNamesActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED " />
            <category android:name="android.intent.category.DEFAULT" />
            <data
                android:host="www.nxp.com"
                android:pathPrefix="/products/identification_and_security/smart_label_and_tag_ics/ntag/series/NT3H1101_NT3H1201.html"
                android:scheme="http" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

            <!-- <action android:name="android.nfc.action.ACTION_TAG_DISCOVERED"/>-->
            <action android:name="android.nfc.action.NDEF_DISCOVERED " />
            <category android:name="android.intent.category.DEFAULT" />
            <data
                android:host="www.nxp.com"
                android:pathPrefix="/products/identification_and_security/smart_label_and_tag_ics/ntag/series/NT3H1101_NT3H1201.html"
                android:scheme="http" />
        </intent-filter>
    </activity>

在我的每个活动中,我都有这段代码来处理 NFC 发现的意图

@Override
protected void onResume() {
    super.onResume();
    ntagHandler.start();
}

@Override
protected void onPause() {
    super.onPause();
    ntagHandler.stop();
}

@Override
protected void onNewIntent(Intent intent) {
    try {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        nfcHandler = new NearFieldCommunicationHandler(tag);
        nfcHandler.connect();

        // DO SOMETHING HERE
        // dataModel.readStableNames(nfcHandler);
    } catch(IOException e) {
        Toast.makeText(this, "Caught exception: " + e.toString(), Toast.LENGTH_LONG).show();
    }
}

您需要实施 "The foreground dispatch system"。它允许 activity 拦截意图并声称优先于其他活动。请参考:https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc#java

First create pendingIntent globally in your each activities as below:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

then take IntentFilter globally and init with null as below:

IntentFilter[] intentFiltersArray = null;

Write below code in onCreate() method:

IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("*/*");    /* YOU CAN TYPE HERE EITHER android.nfc.action.NDEF_DISCOVERED or
            android.nfc.action.ACTION_TAG_DISCOVERED */
        }
        catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        IntentFilter[] intentFiltersArray = new IntentFilter[] {ndef, };

then take techListsArray globally as below:

String[][] techListsArray = new String[][] { new String[] { NfcF.class.getName() } };

最后编写下面的代码以在 activity 失去 (onPause()) 和重新获得 (onResume()) 焦点时启用和禁用前台调度。 enableForegroundDispatch() 必须从主线程调用,并且仅当 activity 位于前台时(调用 onResume() 保证这一点)

public void onPause() {
    super.onPause();
    nfcadapter.disableForegroundDispatch(this);
}

public void onResume() {
    super.onResume();
    nfcadapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);
}

public void onNewIntent(Intent intent) {
    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    //do something with tagFromIntent
}