获取当前活动以处理发现的NFC标签[重复]

问题描述 投票:1回答:1

我有一个Android应用程序,有几个活动。我希望当前活动处理nfc发现的事件,因为应用程序的状态决定了我想对标记做什么

从附加代码中可以看出,我已经在每个活动上设置了意图,并在每个活动中实现了onResume,onPause和onNewIntent方法。

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

你们有任何想法如何让积极的活动来处理被发现的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();
    }
}
java android nfc
1个回答
0
投票

您需要实现“前台调度系统”。它允许活动拦截意图并声明优先于其他活动。请参考:https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc#java

首先在每个活动中全局创建pendingIntent,如下所示:

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

然后全局使用IntentFilter,使用null获取null,如下所示:

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, };

然后在全球范围内使用techListsArray,如下所示:

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

最后写下面的代码,以便在活动失败(onPause())和重新获得(onResume())焦点时启用和禁用前台调度。必须从主线程调用enableForegroundDispatch(),并且只有当活动位于前台时(调用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
}
© www.soinside.com 2019 - 2024. All rights reserved.