【问题标题】:NFC Tag Reading Activity not receiving intentNFC 标签读取活动未收到意图
【发布时间】:2021-01-16 06:39:11
【问题描述】:

我有一个读取 NFC A 类标签(非 NDEF)的活动。我正在开发者模式下使用 Android 手机运行 Android Studio。

该项目在手机上打开 NFC 的情况下在开发者 Android 手机上正确启动应用程序。当我在手机上点击我的 NFC 非接触式卡时,手机会检测到 NFC 卡,但会显示手机上安装的其他 NFC 读取器应用程序的选项列表,而不是将意图传递给前台应用程序。

如何让前台项目的应用程序接收 NFC Intent 而不是弹出建议列表?

这是我的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>

这是 Activity Java 类:

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 读卡器应用的建议列表?

【问题讨论】:

    标签: java android nfc


    【解决方案1】:

    我发现我必须在onResume() 中设置enableForegroundDispatch() 参数,而不是保留空值。

    工作代码:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    相关资源
    最近更新 更多