onCreate():
// Create a PendingIntent object so the Android system can populate it with the details of the tag when it is scanned.
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Declare intent filters to handle the intents that the developer wants to intercept.
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
ndef.addDataType("text/plain");
intentFiltersArray = new IntentFilter[] {ndef};
public void onResume() {
super.onResume();
// Enable the foreground dispatch when the Activity regains focus.
NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null);
}
public void onPause() {
super.onPause();
// Disable the foreground dispatch when the Activity loses focus.
NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}