我有一个tabactivity,点击每个标签会调用一个新的活动。在每项活动中,我都可以选择点击NFC卡并获取价值。但问题是当我在手机上点击卡片时,会调用一个新的活动,而不会显示父项。代码在非tabactivity中运行完美。
这是我的代码。
adapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[] { tagDetected };
ctx=this
Now from drop down option I select tag
if(position == 2){
mMsisdn_layout.setVisibility(View.GONE);
mNFC_layout.setVisibility(View.VISIBLE);
if(!adapter.isEnabled()){
Toast.makeText(getApplicationContext(), "Please activate NFC and press Back to return to the application!", Toast.LENGTH_LONG).show();
startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
else{
mCustomizeDialog = new CustomizeDialog(this);
mCustomizeDialog.setTitle("NFC");
mCustomizeDialog.setMessage("Please tap your NFC card");
mCustomizeDialog.progressBar.setVisibility(View.INVISIBLE);
mCustomizeDialog.cancelButton.setVisibility(View.GONE);
mCustomizeDialog.okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mytag==null){
mCustomizeDialog.dismiss();
}else{
// mEdtNFC.setText(NfcidValue);
mCustomizeDialog.dismiss();
}
}
});
mCustomizeDialog.show();
}
}
/**
*
* @param text
* @param tag
* @throws IOException
* @throws FormatException
*/
private void write(String text, Tag tag) throws IOException, FormatException {
NdefRecord[] records = { createRecord(text) };
NdefMessage message = new NdefMessage(records);
// Get an instance of Ndef for the tag.
Ndef ndef = Ndef.get(tag);
// Enable I/O
ndef.connect();
// Write the message
ndef.writeNdefMessage(message);
// Close the connection
ndef.close();
}
/**
*
* @param text
* @return
* @throws UnsupportedEncodingException
*/
private NdefRecord createRecord(String text) throws UnsupportedEncodingException {
String lang = "en";
byte[] textBytes = text.getBytes();
byte[] langBytes = lang.getBytes("US-ASCII");
int langLength = langBytes.length;
int textLength = textBytes.length;
byte[] payload = new byte[1 + langLength + textLength];
// set status byte (see NDEF spec for actual bits)
payload[0] = (byte) langLength;
// copy langbytes and textbytes into payload
System.arraycopy(langBytes, 0, payload, 1, langLength);
System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_UNKNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
return recordNFC;
}
/**
*
*/
@Override
protected void onNewIntent(Intent intent){
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Log.d("NFC intent", intent.getAction());
// mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// System.out.println(myta);
Log.d("NFC tag", mytag.toString());
// Log.d("NFC tag",bytesToHex(mytag.getId()).trim());
NfcidValue = ConvertByteToHex.bytesToHex(mytag.getId()).trim();
mEdtNFC.setText(NfcidValue);
// NfcidValue ="";
Toast.makeText(this,"Tap was successful", Toast.LENGTH_LONG ).show();
}
}
@Override
public void onPause(){
super.onPause();
WriteModeOff();
}
/*@Override
public void onResume(){
super.onResume();
}*/
/**
*
*/
private void WriteModeOn(){
if(android.os.Build.VERSION.SDK_INT >= 16){
writeMode = true;
adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
}
}
/**
*
*/
private void WriteModeOff(){
if(android.os.Build.VERSION.SDK_INT >= 16){
writeMode = false;
adapter.disableForegroundDispatch(this);
}
}
此代码工作正常,但我不明白为什么在标签活动中使用它时会调用新活动。
答案 0 :(得分:0)
尝试使用TabActivity
处理NFC意图。
更好的是(从Android UI设计角度来看)将在某种程度上重新设计应用程序,并在单Fragments
ViewPager
内使用Activity
ActionBar
。然后你永远不会遇到这个问题。