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
|
private void resoudreIntent(Intent intent) {
String action = intent.getAction();
if (isActionNFC(action)) {
//Mon action si on rentre
} else {
//Sinon
}
}
private static boolean isActionNFC(String action) {
return NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action);
}
private PendingIntent getPendingIntent() {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, this.getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
return pendingIntent;
}
// Recuperer l'adapter NFC
private NfcAdapter getAdapter() {
NfcManager manager = (NfcManager) getApplicationContext()
.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
return adapter;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.r);
onNewIntent(getIntent());
}
// //////////////////////
// Surdefinition des methodes onnewintent et onresume
// ///////////////
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
resoudreIntent(intent);
super.onNewIntent(intent);
}
@Override
public void onResume() {
PendingIntent pendingIntent = getPendingIntent();
if (getAdapter() != null) {
getAdapter().enableForegroundDispatch(this, pendingIntent, null,
null);
}
resoudreIntent(getIntent());
super.onResume();
} |
Partager