当用户扫描 nfc 标签时,我需要同时打开我的应用程序并阅读 nfc 消息。
目前我可以在用户点击或扫描 nfc 时打开我的,但我无法同时读取 nfc 中的数据。
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi") 公共类 MainActivity 扩展 Activity {
public static final String TAG = "NfcDemo";
private TextView mTextView;
// private NfcAdapter mNfcAdapter;
protected PendingIntent nfcPendingIntent;
NfcAdapter nfcAdapter;
IntentFilter[] readTagFilters;
PendingIntent pendingIntent;
Tag detectedTag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView1);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
detectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(
NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter filter2 = new IntentFilter(
NfcAdapter.ACTION_NDEF_DISCOVERED);
readTagFilters = new IntentFilter[] { tagDetected, filter2 };
Log.d(TAG, "ok" + getIntent().getAction());
}
public void enableForegroundMode() {
Log.d(TAG, "enableForegroundMode");
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
Log.d("done ", "in " + tagDetected.getAction(0));
IntentFilter[] writeTagFilters = new IntentFilter[] { tagDetected };
}
@Override
protected void onResume() {
Log.d(TAG, "onResume");
super.onResume();
nfcAdapter.enableForegroundDispatch(this, pendingIntent,readTagFilters, null);
Intent ndefDetected = new Intent(NfcAdapter.ACTION_TAG_DISCOVERED);
Tag tag = ndefDetected.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Parcelable[] rawMsgs = ndefDetected .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
Log.d(TAG, "onResume1");
} else {
Log.d(TAG, "onResume2");
}
}
@Override
protected void onPause() {
Log.d(TAG, "onPause");
super.onPause();
if (NfcAdapter.getDefaultAdapter(this) != null)
NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
setIntent(intent);
Log.d(TAG, "NewIntent is ...." + intent.getAction() + "....."
+ NfcAdapter.ACTION_NDEF_DISCOVERED);
getTagMsg(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void getTagMsg(Intent intent) {
Log.d(TAG, "getTagContent" + intent.getAction() + "....."
+ NfcAdapter.ACTION_NDEF_DISCOVERED);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Log.d(TAG, "New Intent/1");
NdefMessage[] messages = null;
Parcelable[] rawMsgs = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
messages = new NdefMessage[rawMsgs.length];
Log.d(TAG, "New Intent/2:" + messages);
for (int i = 0; i < rawMsgs.length; i++) {
messages[i] = (NdefMessage) rawMsgs[i];
}
}
if (messages[0] != null) {
String result = "";
byte[] payload = messages[0].getRecords()[0].getPayload();
// this assumes that we get back am SOH followed by host/code
for (int b = 0; b < payload.length; b++) { // skip SOH
result += (char) payload[b];
}
Log.d(TAG, "New Intent/3:" + result);
Toast.makeText(getApplicationContext(),
"Tag Contains " + result, Toast.LENGTH_SHORT).show();
}
}
}
}