专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 移动开发

现阶段应用拦截NFC响应,不弹出选择框教程

发布时间:2010-05-30 00:38:13 文章来源:www.iduyao.cn 采编人员:星星草
当前应用拦截NFC响应,不弹出选择框教程

从上一篇的NFC支持所类型的卡读取之后,下面要解决的就是NFC的拦截响应,如果这一步没有做,当系统内有多个支持NFC的应用的时候,就会在nfc刷卡的时候弹出多个应用选择,我们需要的场景是,当前应用需要用NFC才去刷卡,然后本应用拦截intent分发(Using the Foreground Dispatch System).

具体的方案:

1.创建PendingIntent来分发要响应的Activity

  mPendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, NFCActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

2.当刷卡的时候,使用intent过滤器来过滤出你要拦截的Intent

  IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        intentFiltersArray = new IntentFilter[]{ndef,};

3.设置你要处理的tag technologies到String数组中

techListsArray = new String[][]{new String[]{NfcA.class.getName()}};

4.在onResume和onPause中设置NFCAdapter

public void onPause() {
    super.onPause();
    mAdapter.disableForegroundDispatch(this);
}

public void onResume() {
    super.onResume();
    mAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);
}
当然之前要定义NfcAdapter

 

  nfcAdapter = NfcAdapter.getDefaultAdapter(this);
这个问题的解决体会到一件事,这个问题在国内的博客论坛都没有搜索到,在StackOverFlow上有一篇

http://stackoverflow.com/questions/16542147/how-to-read-nfc-tags-to-prevent-system-dialogs-nfc-starter-list-and-tags-being-d

里面的链接指向的是Android develop。。。NFC foreground dispatching

所有的基础教程都可以在developer中找到,以后再也不瞎折腾了,有问题找google




友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: