java - 如何在 NDEF 发现/Activity 启动后显示 NDEF 消息?

标签 java android nfc mifare

已经阅读了一些与我类似的问题 - 如果我错过了解决我的困境的方法,我只能道歉,但我真的无法解决这个问题!

我已经设法让我的 nfc Activity 正常工作 - 点击标签会启动我的应用程序的正确 Activity - 但未显示 ndef 消息,我不明白为什么......

该标签是带有纯文本消息编码的 Mifare Ultralight。 (mime类型:纯文本/文本)

这是我的代码,谢谢大家的帮助,这个论坛应该有一个“捐赠啤酒”按钮!

package com.example.prototype02;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.Toast;

public class nfcActivity extends Activity {
    private static final String TAG = "NFCReadTag";
    private NfcAdapter mNfcAdapter;
    private IntentFilter[] mNdefExchangeFilters;
    private PendingIntent mNfcPendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nfclayout);

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

        mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);


        IntentFilter nfcIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        nfcIntent.addDataScheme("text/plain");      
        mNdefExchangeFilters = new IntentFilter[] { nfcIntent };

    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);      
        if (mNfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            NdefMessage[] messages = null;
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(mNfcAdapter.EXTRA_NDEF_MESSAGES);
            if (rawMsgs != null) {
                messages = new NdefMessage[rawMsgs.length];
                for (int i = 0; i < rawMsgs.length; i++) {
                    messages[i] = (NdefMessage) rawMsgs[i];
                }}
                else if (rawMsgs == null){
                    Toast.makeText(getApplicationContext(), "No NDEF Message Read", Toast.LENGTH_LONG).show();
                }
            if(messages[0] != null) {
                String result="";
                byte[] payload = messages[0].getRecords()[0].getPayload();
                for (int b = 1; b<payload.length; b++) { // skip SOH
                    result += (char) payload[b];
                }
                Toast.makeText(getApplicationContext(), "Safe Location Registered - " + result, Toast.LENGTH_SHORT).show();
            }
        }
        else Toast.makeText(getApplicationContext(), "Intent Error...", Toast.LENGTH_LONG).show();

        }
    }

最佳答案

如果您的 Activity 是由 NFC Intent 启动的,则不会调用 onNewIntent() (只有在 Activity 已处于前台且扫描标签时才会调用它)。尝试执行类似在 onCreate() 中调用 onNewIntent(getIntent()) 的操作。

关于java - 如何在 NDEF 发现/Activity 启动后显示 NDEF 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14222831/

相关文章:

android - 在 Android 2.3.5 上,NullPointerException 当 enableForegroundDispatch(...)

android - 通过伪造 NFC 标签扫描来测试应用程序

java - 打印给定的ip打印机

android - Retrofit 2.0 解析 HTML

java - 如何在两个项目中的测试文件夹之间设置 Maven 依赖关系?

java - Android - Ipay88 出现错误访问被拒绝查找属性 "ro.serialno"

android - 自定义 SurfaceView 导致 NoSuchMethodException

controller - NFC 前端与 NFC Controller - 有什么区别?

java - Spring反序列化带有额外列的manytomany表

java - Android - 如何使选中一个复选框取消选中另一个?