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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| public class NfcBaseActivity extends Activity{
private final static String TAG = NfcBaseActivity.class.getSimpleName();
private PendingIntent mPendingIntent;
private static IntentFilter[] mIntentFiltersArray;
private static String[][] mTechListsArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate, action : " + getIntent().getAction());
//Get NFC ADAPTER (if NFC enabled)
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
Alert(R.string.nfc_error, R.string.no_nfc);
}
// Android system will populate it with the details of the tag when it is scanned
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
//Launched by tag scan ?
Tag tag = (Tag)getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
if(tag != null && (getIntent().getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)
|| getIntent().getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)
|| getIntent().getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED))){
Log.i(TAG, "onCreate, tag found, calling onNewTag");
getNewTag(tag, getIntent());
//Clear intent
setIntent(null);
}
}
//Static initialization
static{
// add intent filter
IntentFilter mndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter mtech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
IntentFilter mtag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
try {
//Handles all MIME based dispatches !!! specify only the ones that you need.
mndef.addDataType("*/*");
}
catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mIntentFiltersArray = new IntentFilter[] {mndef,mtech,mtag };
//array of TAG TECHNOLOGIES that your application wants to handle
mTechListsArray = new String[][] { new String[] { NfcA.class.getName()},
new String[] {NfcB.class.getName()},
new String[] {NfcV.class.getName()},
new String[] {IsoDep.class.getName()},
new String[] {Ndef.class.getName()}};
}
private void getNewTag(Tag tag, Intent intent){
if(tag == null) return;
//Indicate to childs that a new tag has been detected
onNewTag(tag);
}
@Override
public void onNewIntent(Intent intent){
Log.i(TAG, "onNewIntent : " + intent.getAction());
// get the tag object for the discovered tag
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
getNewTag(tag, intent);
}
//This function is called in child activities when a new tag is scanned.
public void onNewTag(Tag tag){}
@Override
public void onPause() {
super.onPause();
Log.i(TAG, "onPause");
mAdapter.disableForegroundDispatch(this);
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume");
//treats all incoming intents when a tag is scanned and the appli is in foreground
mAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFiltersArray, mTechListsArray);
}
} |
Partager