我正在尝试这样做,以便当我打开应用程序并扫描标签时,它将显示带有标签ID的祝酒词。问题是,每次我扫描标签时,它都会显示一个应用程序列表,我可以在其中选择我的应用程序。

我尝试实现前台分派机制,但随后它根本不显示任何内容(既不会崩溃,也不会显示Toast)。

这是主要代码:

public class loginPage extends AppCompatActivity {
    NfcAdapter mAdapter;
    PendingIntent pendingIntent;
    private String serialId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_page);
        mAdapter = NfcAdapter.getDefaultAdapter(this);
        pendingIntent = PendingIntent.getActivity(
                this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        if (mAdapter == null) {
            Toast.makeText(this,
                    "NFC NOT supported on this devices!",
                    Toast.LENGTH_LONG).show();
            finish();
        } else if (!mAdapter.isEnabled()) {
            Toast.makeText(this,
                    "NFC NOT Enabled!",
                    Toast.LENGTH_LONG).show();
            finish();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
    }
    @Override
    public void onPause(){
        super.onPause();
        mAdapter.disableForegroundDispatch(this);

    }
    @Override
    public void onNewIntent(Intent intent) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String action = intent.getAction();
        if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
            try {
                byte[] tagId = tag.getId();
                serialId = toHexString(tagId);
                Log.d("[ReadCardTools]", "Serial Number: " + serialId);
                Toast.makeText(this, serialId, Toast.LENGTH_SHORT).show();
            } catch (NullPointerException ex) {
                ex.printStackTrace();
                serialId = "ERROR";
            }
        }
    }

    public static String toHexString(byte[] bytes) {
        char[] hexArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for (int j = 0; j < bytes.length; j++) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v / 16];
            hexChars[j * 2 + 1] = hexArray[v % 16];
        }
        return new String(hexChars);
    }
}


清单包含以下附加内容:

<uses-permission android:name="android.permission.NFC"/>
<uses-feature android:name="android.hardware.nfc"
    android:required="true"/>




<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" />


过滤器为:

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


每次我扫描标签时,logcat仅显示“ D / PersonaManager:isNFCAllowed”。

最佳答案

前台分派系统在启用为包罗万象时(即将方法enableForegroundDispatch()的最后两个参数设置为null)将向您的活动发送TAG_DISCOVERED意图。由于您仅侦听TECH_DISCOVERED意图(而忽略所有其他意图),因此不会在标记发现时显示吐司。

因此,您还需要在onNewIntent中收到TAG_DISCOVERED意向后采取行动:

@Override
public void onNewIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
        NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (tag != null) {
            serialId = toHexString(tag.getId());
            Log.d("[ReadCardTools]", "Serial Number: " + serialId);
            Toast.makeText(this, serialId, Toast.LENGTH_SHORT).show();
        }
    }
}

关于android - NFC前台调度不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45709017/

10-09 01:43