NFC (appli pas au premier plan)
Bonjour,
Je rencontre un problème sur la lecture des tags NFC.
Quand mon appli est lancée, je vois bien les tags, pas de soucis.
Quand on est sur le menu principal, si je passe un tag contenant des données, l'appli taginfo se lance sans qu'on me demande si je préfère utiliser la mienne.
Quand le tag ne contient pas de données, j'ai le choix entre mon appli et l'autre.
J'ai pourtant mis ceci dans mon manifest:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter> |
Du coté de l'activité, j'ai bien mis ceci:
Code:
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();
} |
Je pense avoir codé les choses necessaires...
Quelqu'un aurait une idée?
Merci à vous