首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NDEF发现/活动启动后如何显示NDEF消息?

NDEF发现/活动启动后如何显示NDEF消息?
EN

Stack Overflow用户
提问于 2013-01-09 03:44:33
回答 2查看 3.4K关注 0票数 0

我已经通读了许多和我的问题类似的问题--如果我没有找到解决问题的办法,我只能道歉,但我真的解决不了这个问题!

我已经设法让我的nfc活动正常工作了--点击标签就会启动我的应用程序的正确活动--但是ndef消息没有显示,我不知道为什么……

该标记是Mifare Ultralight,其中包含编码的纯文本消息。(mimetype:纯/文本)

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

代码语言:javascript
复制
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();

        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-09 05:54:21

如果您的活动是由NFC意图启动的,则不会调用onNewIntent() (只有在活动已经在前台时扫描标记时才会调用它)。尝试执行一些操作,比如在onCreate()中调用onNewIntent(getIntent())

票数 1
EN

Stack Overflow用户

发布于 2013-01-18 23:34:59

尝试如下所示:(摘自StickyNotes NFC示例)

代码语言:javascript
复制
@Override
protected void onResume() {
    super.onResume();
    mResumed = true;
    // Sticky notes received from Android
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        NdefMessage[] messages = getNdefMessages(getIntent());
        byte[] payload = messages[0].getRecords()[0].getPayload();
        setNoteBody(new String(payload));
        setIntent(new Intent()); // Consume this intent.
    }
    enableNdefExchangeMode();
}

@Override
protected void onPause() {
    super.onPause();
    mResumed = false;
    mNfcAdapter.disableForegroundNdefPush(this);
}

@Override
protected void onNewIntent(Intent intent) {
    // NDEF exchange mode
    if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        NdefMessage[] msgs = getNdefMessages(intent);
        promptForContent(msgs[0]);
    }

    // Tag writing mode
    if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        writeTag(getNoteAsNdef(), detectedTag);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14222831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档