1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
public class EasyTag extends Activity {
private final boolean DEBUG = true;
private final String TAG = "EasyTag Debug";
NfcAdapter mNfcAdapter;
TextView textID;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
if(DEBUG)Log.i(TAG, "onCreate");
textID = (TextView)findViewById(R.id.textViewID);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNfcAdapter = NfcAdapter.getDefaultAdapter();
if(DEBUG)Log.i(TAG, "NfcAdapter.getDefaultAdapter()");
if(mNfcAdapter == null){
if(DEBUG)Log.i(TAG, "no NFC Adapter");
Toast.makeText(this, "no NFC Adapter", Toast.LENGTH_LONG)
.show();
finish();
}
}
@Override
protected void onStart() {
if(DEBUG)Log.i(TAG, "onStart");
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onPause() {
if(DEBUG)Log.i(TAG, "onPause");
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onResume() {
if(DEBUG)Log.i(TAG, "onResume");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
detection(this.getIntent());
super.onResume();
}
@Override
protected void onStop() {
if(DEBUG)Log.i(TAG, "onStop");
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onDestroy() {
if(DEBUG)Log.i(TAG, "onDestroy");
// TODO Auto-generated method stub
super.onDestroy();
}
public void detection(Intent intent){
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
if(DEBUG)Log.i(TAG, "TAG_DISCOVERED");
Tag tagID = intent.getParcelableExtra(NfcAdapter.EXTRA_ID);
textID.setText("ID: "+tagID.getId());
}
} |
Partager