NFC标签阅读 [英] NFC Tag Reading

查看:158
本文介绍了NFC标签阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的读取和写入NFC标签与NDEF数据格式的应用程序。这一切都似乎是确定。但每当我试图接近一个标签到我的手机,它会产生一个新的活动。我只是想获得对标签数据,而无需打开新的意图。所以,我可以让我的应用程序决定变量是否已经被挖掘两次连胜。

在code,我处理未来标签:

 公共类MainActivity延伸活动{

公共静态语境myContext;
私人按钮myButton的;
私人诠释currentID,currentBalance;

保护无效的onCreate(包savedInstanceState){//首先创建时,
                                                        //标签轻拍
                                                        // 手机
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    为myButton =(按钮)findViewById(R.id.button1);

    currentID = 0;
    currentBalance = 0;


    myButton.setOnClickListener(新View.OnClickListener(){//标签写入
                                                                // 功能
                                                                //地方
                                                               // 这里

        公共无效的onClick(查看为arg0){

            意图I =新的意图(getApplicationContext(),nfcWrite.class);
            i.putExtra(ID,currentID);
            i.putExtra(平衡,currentBalance);
            startActivity(ⅰ);

        }
    });

}



@覆盖
保护无效onResume(){
    // TODO自动生成方法存根
    super.onResume();


    意向意图= getIntent();

    如果(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent()。的getAction())){
        Parcelable [] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefRecord myRecord =((NdefMessage)rawMsgs [0])getRecords()[0];
        字符串nfcData =新的String(myRecord.getPayload());
        串[] splitData = nfcData.split( - );
        currentID =的Integer.parseInt(splitData [0]);
        currentBalance =的Integer.parseInt(splitData [1]);

        Toast.makeText(getApplicationContext(),nfcData,Toast.LENGTH_LONG).show();
    }


}

}
 

解决方案

添加安卓launchMode =singleTask<活性GT; 元素 MainActivity 。需要注意的是,如果该活动已经在运行, onNewIntent()将被调用,而不是的onCreate(),以提供在NDEF 意图给你。因此,您将需要处理的NDEF 意图在两个的onCreate()(如果该活动尚未运行)和 onNewIntent()(如果该活动已在运行)。

说明了该技术的示例项目。

I got an application that reads and writes nfc tags with ndef data format. This all seems to be ok. But whenever i try to get closer a tag to my phone, it produces a new activity. I only want to obtain data on the tag without opening new intent. So i can make my app decide whether the tag has been tapped two times in a row.

The code that i handle coming tags:

public class MainActivity extends Activity {

public static Context myContext;
private Button myButton;
private int currentID, currentBalance;

protected void onCreate(Bundle savedInstanceState) { // Firstly Created when
                                                        // a tag tapped to
                                                        // the phone
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myButton = (Button) findViewById(R.id.button1);

    currentID = 0;
    currentBalance = 0;


    myButton.setOnClickListener(new View.OnClickListener() { // Tag write
                                                                // function
                                                                // places
                                                               // here

        public void onClick(View arg0) {

            Intent i = new Intent(getApplicationContext(), nfcWrite.class);
            i.putExtra("id",currentID);
            i.putExtra("balance",currentBalance);
            startActivity(i);

        }
    });

}



@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();


    Intent intent = getIntent();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefRecord myRecord = ((NdefMessage) rawMsgs[0]).getRecords()[0];
        String nfcData = new String(myRecord.getPayload());
        String[] splitData = nfcData.split("-");
        currentID = Integer.parseInt(splitData[0]);
        currentBalance = Integer.parseInt(splitData[1]);

        Toast.makeText(getApplicationContext(), nfcData, Toast.LENGTH_LONG).show();
    }


}

}

解决方案

Add android:launchMode="singleTask" to the <activity> element for MainActivity. Note that if the activity is already running, onNewIntent() will be called, instead of onCreate(), to deliver the NDEF Intent to you. Hence, you will need to handle the NDEF Intent in both onCreate() (if the activity was not already running) and onNewIntent() (if the activity was already running).

This sample project illustrates the technique.

这篇关于NFC标签阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆