【问题标题】:NFC Tag ReadingNFC 标签读取
【发布时间】:2014-04-25 19:58:16
【问题描述】:

我有一个使用 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 android-intent nfc


    【解决方案1】:

    android:launchMode="singleTask" 添加到<activity> 元素的MainActivity。请注意,如果活动已在运行,则将调用 onNewIntent() 而不是 onCreate(),以将 NDEF Intent 传递给您。因此,您需要在onCreate()(如果活动尚未运行)和onNewIntent()(如果活动已经在运行)中处理 NDEF Intent

    This sample project 说明了这项技术。

    【讨论】:

      【解决方案2】:

      不是将接收 NFC 意图的 Activity 的启动模式设置为“singleTask”(参见 CommonsWare 的答案),NFC 应用程序在前台接收 NFC 相关意图的首选方式是 @987654321 @(或仅使用 Android 4.4 时的 reader mode API)。

      为了使用前台调度,您需要像这样创建一个待处理的意图:

      PendingIntent pendingIntent = PendingIntent.getActivity(
          this,
          0,
          new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
          0);
      

      然后,在您的活动的onResume() 方法中,您将启用前台调度,如下所示:

      NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
      nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
      

      然后您将通过活动的onNewIntent() 方法收到通知您发现标签的意图:

      public void onNewIntent(Intent intent) {
          ...
      }
      

      此外,您必须在 Activity 的 onPause() 方法中禁用前台调度:

      NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
      nfcAdapter.disableForegroundDispatch(this);
      

      【讨论】:

      • 这两种技术并不相互排斥。 enableForegroundDispatch() 仅解决应用程序处于前台时的情况。如果应用程序在后台(但仍在运行),扫描一个新的匹配 NDEF 标记仍将创建一个新的活动实例,除非您使用类似 singleTask 的东西来尝试缓解这种情况。
      • @CommonsWare 真的!我没有考虑这种情况。
      猜你喜欢
      • 1970-01-01
      • 2019-04-19
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      相关资源
      最近更新 更多