从上一篇的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);
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;import com.hwuao.ocis.R;
import com.hwuao.ocis.components.easyAndroid.BaseActivity;
import com.hwuao.ocis.components.easyAndroid.ContentView;
import com.hwuao.ocis.components.easyAndroid.tool.AppLogger;
import com.hwuao.ocis.event.GetNFCCardEvent;
import com.hwuao.ocis.util.StringUtils;
import com.hwuao.ocis.util.ToastUtil;/*** NFC响应页面** @author Jinyang*/
@SuppressLint("NewApi")
@ContentView(R.layout.activity_nfc)
public class NFCActivity extends BaseActivity {public static final String TAG = "NFCActivity";private NfcAdapter nfcAdapter;private String mClientCardNfcId;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initNfcParse();resolveIntent(getIntent());}private void initNfcParse() {nfcAdapter = NfcAdapter.getDefaultAdapter(this);if (nfcAdapter == null) {AppLogger.e("---->Nfc error !!!");Toast.makeText(getApplicationContext(), "不支持NFC功能!", Toast.LENGTH_SHORT).show();} else if (!nfcAdapter.isEnabled()) {AppLogger.e("---->Nfc close !!!");Toast.makeText(getApplicationContext(), "请打开NFC功能!", Toast.LENGTH_SHORT).show();}}@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);resolveIntent(intent);}@Overridepublic void onResume() {super.onResume();AppLogger.e("---->onDestroy");if (nfcAdapter != null) {if (!nfcAdapter.isEnabled()) {showWirelessSettingsDialog();}}}private void showWirelessSettingsDialog() {AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage("不支持此卡");builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialogInterface, int i) {Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);startActivity(intent);}});builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialogInterface, int i) {finish();}});builder.create().show();return;}private void resolveIntent(Intent intent) {String action = intent.getAction();if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);String nfcId = dumpTagData(tag);if (!nfcId.isEmpty()) {mClientCardNfcId = nfcId;Log.i(TAG, "卡的内容" + mClientCardNfcId);eventBus.post(new GetNFCCardEvent(mClientCardNfcId));finish();} else {ToastUtil.showToastLong(getApplicationContext(), "识别失败!请重新刷卡!");}}}private String dumpTagData(Parcelable p) {Tag tag = (Tag) p;byte[] id = tag.getId();return StringUtils.hexToHexString(id);}}

这个问题的解决体会到一件事,这个问题在国内的博客论坛都没有搜索到,在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

源码请见

https://github.com/xujinyang/NFCALL

当前应用拦截NFC响应,不弹出选择框教程相关推荐

  1. JSP页面的日期控件可以弹出选择框选择日期

    JSP页面的日期控件可以弹出选择框选择日期 input框的如下 <input type="text" name="starttime" readonly= ...

  2. jQuery日期弹出选择框Datepicker效果

    无论你是一个机票在线预定网站设计师,还是一个工程任务管理者,抑或在你的注册表单上有个生日填写项目:本文即将提到的日历日期选择弹出窗口都将帮助你简化用户操作,提高网站的用户体验和易用性. 教程目标:教会 ...

  3. wpf 点击按钮弹出选择框_关于WPF的弹出窗口

    几个重要的概念需要清楚: Show和ShowDialog区别 1.调用Show方法后弹出子窗口后,线程会继续往下执行.调用ShowDialog方法弹出子窗口后,线程会阻塞,直到子窗口关闭才继续往下执行 ...

  4. MUI框架div模拟按钮(日期选择器)导致双击才能弹出选择框,移动端Web不支持dblclick双击的问题。

    一些前任项目遗留的问题,mui坑太多,不知道为什么要用div模拟日期选择的button按钮,导致使用时点击2次才能弹出日期选择框,但是他之前的项目都没问题,可以单击弹出,浪费了我大量时间排查原因. ★ ...

  5. wpf 点击按钮弹出选择框_WPF-PopupWindow wpf右下角弹出框,通过按钮调用,类似QQ CSharp C#编程 238万源代码下载- www.pudn.com...

    文件名称: WPF-PopupWindow下载 收藏√  [ 5  4  3  2  1 ] 开发工具: C# 文件大小: 90 KB 上传时间: 2013-07-24 下载次数: 19 详细说明:w ...

  6. Android中用两层AlertDialog来进行弹出选择框信息选择

    在Android经常会用到AlertDialog,把内容使用AlertDialog结合列表的形式显示出来,然后我们点击得到点击的信息. 这里可以使用两层的AlertDialog来实现 1:我们现在xm ...

  7. appium+python自动化98-非select弹出选择框定位解决

    前言 遇到问题:document.getElementsByClassName(...)[0] is undefined 选择框如果是select标签的,可以直接用select专用的方法去定位点击操作 ...

  8. Android底部弹出选择框PickerView的使用

    希望大家可以留个关注点赞,后续会有更多的技术分享 前言 本次主要介绍Android中底部弹出框的使用,使用两个案例来说明,首先是时间选择器,然后是自定义底部弹出框的选择器,以下来一一说明他们的使用方法 ...

  9. Android开发- 点击按钮旁弹出选择框

    示例: 一.代码部分: 1.弹出框页面布局(layout/pop_mesure_layout.xml) <!--LinearLayout不能设置背景颜色--> <LinearLayo ...

最新文章

  1. java动态打jar包_java动态加载jar包
  2. 解密京东618大促数据库运维的攻守之道
  3. linux 卸载yaf,LINUX操作系统怎么搭建YAF框架
  4. ShareEntryActivity java.lang.ClassNotFoundException | Android类找不到问题
  5. 在eclipse中创建web项目(非myeclipse)
  6. 0xc0000225无法进系统_电脑无法启动,出现0xc0000225一到错误,该怎样解决!
  7. hibernate 标识符_Hibernate中的标识符
  8. android.mk简单介绍
  9. 正则中关于环视(lookaround)的小例子
  10. 「BZOJ 2142」礼物
  11. vaex 处理海量数据_爱了爱了!0.052 秒打开 100GB 数据,这个Python开源库火爆了!...
  12. 树莓派 4b 可执行文件 无法双击运行_树莓派01 - 树莓派系统安装
  13. InveighZero:基于C#的数据欺骗和MitM工具
  14. 数字孪生数据中心机房,智能 IDC 高阶运维
  15. 对数正态分布随机数c语言,在python中从对数正态分布生成随机数
  16. 今天,是小灰母亲离开的第649天
  17. C语言:输入一个数,输出以该值为半径的圆面积,以该值为半径的球体表面积与体积,pi取值3.1415926536.
  18. 使用Nightwatch.js做基于浏览器的web应用自动测试
  19. 为什么要学习Node.js
  20. 电路中滤波电容和退耦电容_电容的多种作用,定时,耦合,滤波,去耦,微分,分频...

热门文章

  1. 如何大幅提高 Django 网站加载速度
  2. Tensorflow移动端之如何将自己训练的MNIST模型加载到Android手机上
  3. 哪一款iPhone拍照效果最好?拍照最好用的苹果手机是哪款
  4. 鸿蒙对比ios流畅,鸿蒙OS 2.0对比iOS 14:苹果流畅度完败?
  5. 计算机二级考过律,计算机二级考试考完后的这些事情你知道吗?
  6. Linux systemd启动流程
  7. 大小口圆锥台计算机,2011年对口单招计算机试卷A04(18页)-原创力文档
  8. 基于JavaEE的开放平台出租车系统_JSP网站设计_MySQL数据库设计
  9. 2020美容师(初级)操作证考试及美容师(初级)作业模拟考试
  10. 七千年来被国人误解的七句古话