NFC 前台分发机制

NFC两种注册方式: manifest.xml 中注册,在系统发出广播时,所有在manifest.xml中注册的activity都会收到该广播,如果有多个,会提供给用户一个选择界面..........

使用前台分发机制,优先级高于manifest.xml注册方式.

nfc标签前台分发系统

什么叫nfc的前台发布系统?就是说当我们已经打开我们的应用的时候,那么通过这个前台发布系统的设置,
我们可以让我们已经启动的activity拥有更高的优先级来依据我们在代码中定义的标准来过滤和处理intent,而不是让别的声明了intent filter的activity来干扰
,甚至连自己声明在androidManifest中的intent filter都不会来干扰。也就是说foreground Dispatch的优先级大于intent filter。

第一种情况:当你的activity没有启动的时候,去扫描tag,那么系统中所有的intent filter都将一起参与过滤。

第二种情况:当你的actiity启动了,去扫描tag时,那么将直接使用你在foreground dispatch中代码写入的过滤标准。
如果这个标准没有命中任何intent,那么系统将使用所有activity声明的intent filter xml来过滤。

在OnCreate中你可以添加如下代码        

// Create a generic PendingIntent that will be deliver to this activity.
 The NFC stack will fill in the intent with the details of the discovered tag before delivering to this activity.

   mPendingIntent = PendingIntent.getActivity(this, 0,

             new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        

        // 做一个IntentFilter过滤你想要的action 这里过滤的是ndef

        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);

     //生成intentFilter   

        mFilters = new IntentFilter[] {

                ndef

        };

     

        // 做一个tech-list。可以看到是二维数据,每一个一维数组之间的关系是或,但是一个一维数组之内的各个项就是与的关系了

mTechLists =new String[][]{ 
   newString[]{android.nfc.tech.IsoDep.class.getName()}, 
   newString[]{android.nfc.tech.NfcV.class.getName()}, 
   newString[]{android.nfc.tech.NfcF.class.getName()}, 
   newString[]{android.nfc.tech.NfcA.class.getName()}, 
   newString[]{android.nfc.tech.NfcB.class.getName()}, 
   newString[]{android.nfc.tech.NfcBarcode.class.getName()}, 
   newString[]{android.nfc.tech.MifareClassic.class.getName()}, 
   newString[]{android.nfc.tech.MifareUltralight.class.getName()}, 
   };


在onPause和 onResume中需要加入相应的代码。

public void onPause() {

 super.onPause();

取消注册 mAdapter.disableForegroundDispatch(this);

}

 

 public void onResume() {

 super.onResume();

//设定intentfilter和tech-list。如果两个都为null就代表优先接收任何形式的TAG action。也就是说系统会主动发TAG intent。

mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值