1

我正在开发一个 Android 应用程序,它是从 NFC 卡读取 NFC-Tag ID 的功能。我正在使用以下代码从卡中读取 NFC 标签。

@Override
protected void onNewIntent(Intent intent) {
    ReadNFCTagFromIntent(intent);
}

private void ReadNFCTagFromIntent(Intent intent) {
    byte[] inarray = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

    int i, j, in;
        String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "A", "B", "C", "D", "E", "F" };
        String nfc_card_tag = "";
        for (j = 0; j < inarray.length; ++j) {
            in = (int) inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            nfc_card_tag += hex[i];
            i = in & 0x0f;
            nfc_card_tag += hex[i];
        }

    ShowDialog(nfc_card_tag); // This function Will Display a Dialog With Tag Value....
}

我还在活动 OnPause 和 OnResume 活动方法中配置了前台调度系统

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

    try {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, getClass())
                        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter filter = new IntentFilter();
        filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
        filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
        filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.enableForegroundDispatch(this, pendingIntent,
                new IntentFilter[] { filter }, this.techList);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onPause() {
    super.onPause();
    try {
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.disableForegroundDispatch(this);
    } catch (Exception e) {
    }
}

这是清单文件中的我的活动声明

<activity android:name="ActHome"
  android:launchMode="singleTask"
  android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >

        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED" />
        </intent-filter>


        <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfctechlist" />

       <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
       </intent-filter>

这是我用于“TECH_DISCOVERED”的资源文件中的 xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcB</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcF</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcV</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NdefFormatable</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareClassic</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

此代码工作正常,但问题是当我尝试将 nfc 卡连接到我的设备时,我的活动挂起/冻结至少 10/15 秒,然后根据我的代码显示对话框。如果我尝试一张一张地连续连接 5 到 6 张卡片,那么它会在 10 到 15 秒后同时向我显示所有对话框。我不知道为什么我的活动在从 NFC 卡读取标签时挂起。

提前致谢

4

0 回答 0