0
我正在開發一款Android應用程序,在該應用程序中,當點擊該按鈕時需要掃描NFC標籤。但如果我點擊一個按鈕,它沒有檢測到卡,什麼都沒有發生。幫我解決這個問題。Android NFC標籤未檢測到onClick按鈕
public class Check_in extends Activity {
EditText card_details;
PendingIntent pendingIntent;
IntentFilter[] intentFiltersArray;
String[][] techList;
NfcAdapter nfcAdapter;
LinearLayout scanContainer;
private boolean isScanEnabled = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_in);
card_details = (EditText) findViewById(R.id.editText);
}
public void get_nfc(View v)
{
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(nfcAdapter == null)
Toast.makeText(this,"This Device Not supporting NFC",Toast.LENGTH_SHORT).show();
else
handleIntent();
}
private void handleIntent() {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
intentFiltersArray = new IntentFilter[] { ndef };
techList = new String[][] { new String[] { NfcA.class.getName(),
MifareUltralight.class.getName()} };
}
@Override
protected void onResume() {
super.onResume();
if(nfcAdapter !=null) {
if(nfcAdapter.isEnabled())
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
else
Toast.makeText(this,"NFC Disabled",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPause() {
if(nfcAdapter !=null)
nfcAdapter.disableForegroundDispatch(this);
super.onPause();
}
@Override
public void onNewIntent(Intent intent) {
if(!isScanEnabled)
return;
Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
card_details.setText(dumpTagData(tag).toString());
Toast.makeText(this, "This is "+dumpTagData(tag).toString(), 5000).show();
}
private String dumpTagData(Parcelable p) {
StringBuilder sb = new StringBuilder();
Tag tag = (Tag) p;
byte[] id = tag.getId();
sb.append(getHex(id));
return sb.toString();
}
private String getHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i] & 0xff;
if (b < 0x10)
sb.append('0');
sb.append(Integer.toHexString(b));
}
return sb.toString();
}
}
它像一個魅力。它幫助了我很多 – user3805324 2015-04-07 05:38:57