public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) { final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); IntentFilter[] filters = new IntentFilter[1]; String[][] techList = new String[][]{}; // Notice that this is the same filter as in our manifest. filters[0] = new IntentFilter(); filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); filters[0].addCategory(Intent.CATEGORY_DEFAULT); try { filters[0].addDataType(MIME_TEXT_PLAIN); } catch (MalformedMimeTypeException e) { throw new RuntimeException("Check your mime type."); } adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
具体参考:http://stackoverflow.com/questions/28496577/android-how-do-you-disable-that-your-app-reopens-when-scanning-a-nfc-tag
NFC代码中配置ACTION类型
最新推荐文章于 2024-07-25 01:18:27 发布