标签:
使用目的:当前Activity能直接响应NFC标签,而不需要用户在choose所有能处理的Activity。
使用步骤:
第一步:在onCreate()方法中
// NFC前台调度系统 private PendingIntent pendingIntent = null;
...
......
// 初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
第二步:onPause(),onResume(),onNewIntent()方法中添加如下代码。
@Override protected void onPause() { if (nfcAdapter != null) nfcAdapter.disableForegroundDispatch(this); super.onPause(); } @Override protected void onResume() { super.onResume(); if (nfcAdapter != null) nfcAdapter.enableForegroundDispatch(this, pendingIntent, filters, tenchlists); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); // 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来 // 我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // 获取Tag标签,既可以处理相关信息 if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) { Log.d("h_bl", "onNewIntent"); praseIntent(intent); } }
第三部分:关键步骤:onResume()函数中有两个参数还未补充。
void android.nfc.NfcAdapter.enableForegroundDispatch(Activity activity, PendingIntent intent, IntentFilter[] filters, String[][] techLists)
Enable foreground dispatch to the given Activity.
This will give give priority to the foreground activity when dispatching a discovered Tag
to an application.
If any IntentFilters are provided to this method they are used to match dispatch Intents for both the ACTION_NDEF_DISCOVERED
and ACTION_TAG_DISCOVERED
. SinceACTION_TECH_DISCOVERED
relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled by passing in the tech lists separately. Each first level entry in the tech list represents an array of technologies that must all be present to match. If any of the first level sets match then the dispatch is routed through the given PendingIntent. In other words, the second level is ANDed together and the first level entries are ORed together.
If you pass null
for both the filters
and techLists
parameters that acts a wild card and will cause the foreground activity to receive all tags via the ACTION_TAG_DISCOVERED
intent.
This method must be called from the main thread, and only when the activity is in the foreground (resumed). Also, activities must call disableForegroundDispatch(Activity)
before the completion of their onPause()
callback to disable foreground dispatch after it has been enabled.
filters: the IntentFilters to override dispatching for, or null to always dispatch
techLists: the tech lists used to perform matching for dispatching of the ACTION_TECH_DISCOVERED
intent
public String[][] tenchlists; public IntentFilter[] filters; ...
...... tenchlists = new String[][] { { IsoDep.class.getName() }, { NfcV.class.getName() }, { NfcF.class.getName() }, }; try { filters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED, "*/*") }; } catch (MalformedMimeTypeException e1) { e1.printStackTrace(); }
filters:当前activity能过滤的nfc标签。
techLists:应用程序希望处理的NFC标签技术的数组。-- 即要处理的NFC标签技术的数组(获取了,可以不处理,对这个标签没反应)。
标签:
原文地址:http://www.cnblogs.com/H-BolinBlog/p/5378020.html