public class RecordActivity extends BaseActivity {
NfcAdapter mAdapter;
IntentFilter[] mFilters;
PendingIntent mPendingIntent;
public static List recordInformations = new ArrayList();
void resolveIntent(Intent intent) {
// 1) Parse the intent and get the action that triggered this intent
String action = intent.getAction();
// 2) Check if it was triggered by a tag discovered interruption.
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
// 3) Get an instance of the TAG from the NfcAdapter
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] extraID = tagFromIntent.getId();
StringBuilder sb = new StringBuilder();
for (byte b : extraID) {
sb.append(String.format("%02X", b));
}
String tagID = sb.toString();
showToast(tagID);
}
}
String[][] mTechLists = new String[][]{new String[]{NfcA.class.getName()}};
Intent intent = getIntent();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef1 = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
//IntentFilter ndef2 = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
mFilters = new IntentFilter[]{
ndef1,
//ndef2,
};
try {
ndef1.addDataType("*/*");
//ndef2.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (getIntent() != null) {
resolveIntent(getIntent());
}
}
@Override
public void onResume() {
super.onResume();
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
}
@Override
public void onNewIntent(Intent intent) {
Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
resolveIntent(intent);
}
@Override
public void onPause() {
super.onPause();
mAdapter.disableForegroundDispatch(this);
}
}