zoukankan      html  css  js  c++  java
  • 4.NFC前台调度系统

    使用目的:当前Activity能直接响应NFC标签,而不需要用户在choose所有能处理的Activity。

    使用步骤:

    第一步:在onCreate()方法中,创建一个PendingIntent对象

    // 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() {
            if (nfcAdapter != null)
                nfcAdapter.enableForegroundDispatch(this, pendingIntent, filters, tenchlists);
            super.onResume();
        }
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            // 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来
            // 我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法
            Log.d("h_bl", "onNewIntent");
            praseIntent(intent);  //处理Intent
        }

    第三部分:关键步骤:onResume()函数中有两个参数还未补充:filterstechLists

    void android.nfc.NfcAdapter.enableForegroundDispatch(Activity activity, PendingIntent intent, IntentFilter[] filters, String[][] techLists)

    filters:the IntentFilters to override dispatching for, or null to always dispatch。

    前台调度系统activity能过滤的nfc标签,重写能调度的nfc标签过滤器,或者总是填null。

    techLists:the tech lists used to perform matching for dispatching of the ACTION_TECH_DISCOVERED intent

    应用程序希望处理的NFC标签技术的数组。-- 即要处理的NFC标签技术的数组(获取了,可以不处理,对这个标签没反应)。

    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.

    如果filters和techLists参数均为空,则会导致前台activity通过ACTION_TAG_DISCOVERED intent接收所有的标签。

    该两个参数的定义在onCreate()中定义:

        public String[][] tenchlists;
        public IntentFilter[] filters;
    ...
    .......
    // 声明前台activity能处理的NFC标签技术的数组 tenchlists = new String[][] { { IsoDep.class.getName() }, { NfcV.class.getName() }, { NfcF.class.getName() }, }; // 前台activity过滤获得所有的ACTION_TECH_DISCOVERED的intent try { filters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED, "*/*") }; } catch (MalformedMimeTypeException e1) { e1.printStackTrace();
  • 相关阅读:
    mysql字节乱码问题之解决方法
    tracert nbtstat nslookup arp指令用法及示例
    Java ThreadLocal示例及使用方法总结
    C# Undo Redo
    自定义过滤器
    C#粘贴复制数据库中的内容
    分布式数据同步
    创建路径(c#)动态分段
    构建网络数据集(c#)
    创建一个栅格图层
  • 原文地址:https://www.cnblogs.com/H-BolinBlog/p/5378020.html
Copyright © 2011-2022 走看看