Android NFC标签读取

Android NFC标签读取,android,android-intent,nfc,Android,Android Intent,Nfc,我得到了一个应用程序,可以读取和写入具有ndef数据格式的nfc标记。这一切似乎都没问题。但每当我试图让标签靠近我的手机时,它就会产生一个新的活动。我只想获取标签上的数据,而不打开新的意图。因此,我可以让我的应用程序决定标签是否已连续点击两次 我处理即将到来的标记的代码: public class MainActivity extends Activity { public static Context myContext; private Button myButton; private i

我得到了一个应用程序,可以读取和写入具有ndef数据格式的nfc标记。这一切似乎都没问题。但每当我试图让标签靠近我的手机时,它就会产生一个新的活动。我只想获取标签上的数据,而不打开新的意图。因此,我可以让我的应用程序决定标签是否已连续点击两次

我处理即将到来的标记的代码:

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();
    }


}

}

android:launchMode=“singleTask”
添加到
中说明了该技术。

不是将接收NFC意图的活动的启动模式设置为“singleTask”(参见Commonware的答案),而是当应用程序处于前台时,NFC应用程序接收NFC相关意图的首选方式是(或者,当仅使用Android 4.4时,也可以选择此选项)

为了使用前台调度,您将创建一个挂起的意图,如下所示:

PendingIntent pendingIntent = PendingIntent.getActivity(
    this,
    0,
    new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
    0);
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
然后,在活动的
onResume()
方法中,您将如下所示启用前台调度:

PendingIntent pendingIntent = PendingIntent.getActivity(
    this,
    0,
    new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
    0);
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
然后,您将收到意向,通过活动的
onNewIntent()
方法通知您已发现的标记:

public void onNewIntent(Intent intent) {
    ...
}
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
此外,您必须在活动的
onPause()方法中禁用前台分派:

public void onNewIntent(Intent intent) {
    ...
}
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);

这两种技术并不相互排斥。
enableForegroundDispatch()
只处理应用程序位于前台的情况。如果应用程序位于后台(但仍在运行)扫描一个新的匹配的NDEF标签仍然会创建一个新的活动实例,除非你使用诸如“代码> StuttLasks<代码>之类的东西来减轻它。