NFC官方文档学习笔记(一):NFC前台调度

上Android开发官网看下下NFC相关知识,发现在网上相关的介绍也非常多,我也滥竽充数地写一个学习记录,就是官方API DEMO中的COPY版本。
/**
 * NFC前台调度: 读取NDEF数据:一个NFC标签处理与标签的调度系统,分析发现的NFC标签,适当
 * 地对数据进行分类,并启动一个应用程序。在分类的数据中,要处理扫描NFC标签 的应用程序可以声明一个 intent filter来处理数据请求。
 * */
public class ForegroundDispatch extends Activity {
    // NfcAdapter相当于一个NFC适配器,就如同电脑装了网络适配器才可以上网一样,手机上装有NfcAdapter才能使用和NFC相应功能。
    private NfcAdapter mAdapter;
    // pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。
    private PendingIntent mPendingIntent;
    // IntentFilter对象负责过滤掉组件无法响应和处理的Intent,只将自己关心的Intent接收进来进行处理。
    private IntentFilter[] mFilters;
    private String[][] mTechLists;
    private TextView mText;
    private int mCount = 0;

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.foreground_dispatch);
        mText = (TextView) findViewById(R.id.text);
        mText.setText("Scan a tag");
        // NFC前台调度第一步:获取NFC适配器
        // 大部分Android设备只支持一个NFC Adapter,所以一般直接调用getDefaultAapater来获取手机中的Adapter。
        mAdapter = NfcAdapter.getDefaultAdapter(this);

        // 创建一个 PendingIntent, Android系统就能在一个tag被检测到时定位到这个对象
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        // 创建一个IntentFilter来过滤action
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            // 添加过滤的数据类型
            ndef.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        mFilters = new IntentFilter[] { ndef, };

        // 建立NFC s
        mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
    }

    @Override
    public void onResume() {
        super.onResume();
        // NFC前台调度第二步:在onResume()方法中启动前台调度功能
        // 设定intentfilter和tech-list。如果两个都为null就代表优先接收任何形式的TAG
        // action。也就是说系统会主动发TAG intent。
        if (mAdapter != null)
            mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
                    mTechLists);
    }

    @Override
    public void onNewIntent(Intent intent) {
        // 在onNewIntent()用来处理intent数据
        mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);
    }

    @Override
    public void onPause() {
        super.onPause();
        // NFC前台调度第二步: 在Pause()方法中禁用NFC前台调用功能
        if (mAdapter != null)
            mAdapter.disableForegroundDispatch(this);
    }
}
/**
 * NFC前台调度: 读取NDEF数据:一个NFC标签处理与标签的调度系统,分析发现的NFC标签,适当
 * 地对数据进行分类,并启动一个应用程序。在分类的数据中,要处理扫描NFC标签 的应用程序可以声明一个 intent filter来处理数据请求。
 * */
public class ForegroundDispatch extends Activity {
    // NfcAdapter相当于一个NFC适配器,就如同电脑装了网络适配器才可以上网一样,手机上装有NfcAdapter才能使用和NFC相应功能。
    private NfcAdapter mAdapter;
    // pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。
    private PendingIntent mPendingIntent;
    // IntentFilter对象负责过滤掉组件无法响应和处理的Intent,只将自己关心的Intent接收进来进行处理。
    private IntentFilter[] mFilters;
    private String[][] mTechLists;
    private TextView mText;
    private int mCount = 0;

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.foreground_dispatch);
        mText = (TextView) findViewById(R.id.text);
        mText.setText("Scan a tag");
        // NFC前台调度第一步:获取NFC适配器
        // 大部分Android设备只支持一个NFC Adapter,所以一般直接调用getDefaultAapater来获取手机中的Adapter。
        mAdapter = NfcAdapter.getDefaultAdapter(this);

        // 创建一个 PendingIntent, Android系统就能在一个tag被检测到时定位到这个对象
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        // 创建一个IntentFilter来过滤action
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            // 添加过滤的数据类型
            ndef.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        mFilters = new IntentFilter[] { ndef, };

        // 建立NFC s
        mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
    }

    @Override
    public void onResume() {
        super.onResume();
        // NFC前台调度第二步:在onResume()方法中启动前台调度功能
        // 设定intentfilter和tech-list。如果两个都为null就代表优先接收任何形式的TAG
        // action。也就是说系统会主动发TAG intent。
        if (mAdapter != null)
            mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
                    mTechLists);
    }

    @Override
    public void onNewIntent(Intent intent) {
        // 在onNewIntent()用来处理intent数据
        mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);
    }

    @Override
    public void onPause() {
        super.onPause();
        // NFC前台调度第二步: 在Pause()方法中禁用NFC前台调用功能
        if (mAdapter != null)
            mAdapter.disableForegroundDispatch(this);
    }
}
/**
 * NFC前台调度: 读取NDEF数据:一个NFC标签处理与标签的调度系统,分析发现的NFC标签,适当
 * 地对数据进行分类,并启动一个应用程序。在分类的数据中,要处理扫描NFC标签 的应用程序可以声明一个 intent filter来处理数据请求。
 * */
public class ForegroundDispatch extends Activity {
    // NfcAdapter相当于一个NFC适配器,就如同电脑装了网络适配器才可以上网一样,手机上装有NfcAdapter才能使用和NFC相应功能。
    private NfcAdapter mAdapter;
    // pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。
    private PendingIntent mPendingIntent;
    // IntentFilter对象负责过滤掉组件无法响应和处理的Intent,只将自己关心的Intent接收进来进行处理。
    private IntentFilter[] mFilters;
    private String[][] mTechLists;
    private TextView mText;
    private int mCount = 0;

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.foreground_dispatch);
        mText = (TextView) findViewById(R.id.text);
        mText.setText("Scan a tag");
        // NFC前台调度第一步:获取NFC适配器
        // 大部分Android设备只支持一个NFC Adapter,所以一般直接调用getDefaultAapater来获取手机中的Adapter。
        mAdapter = NfcAdapter.getDefaultAdapter(this);

        // 创建一个 PendingIntent, Android系统就能在一个tag被检测到时定位到这个对象
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        // 创建一个IntentFilter来过滤action
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            // 添加过滤的数据类型
            ndef.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        mFilters = new IntentFilter[] { ndef, };

        // 建立NFC s
        mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
    }

    @Override
    public void onResume() {
        super.onResume();
        // NFC前台调度第二步:在onResume()方法中启动前台调度功能
        // 设定intentfilter和tech-list。如果两个都为null就代表优先接收任何形式的TAG
        // action。也就是说系统会主动发TAG intent。
        if (mAdapter != null)
            mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
                    mTechLists);
    }

    @Override
    public void onNewIntent(Intent intent) {
        // 在onNewIntent()用来处理intent数据
        mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);
    }

    @Override
    public void onPause() {
        super.onPause();
        // NFC前台调度第二步: 在Pause()方法中禁用NFC前台调用功能
        if (mAdapter != null)
            mAdapter.disableForegroundDispatch(this);
    }
}
/**
 * NFC前台调度: 读取NDEF数据:一个NFC标签处理与标签的调度系统,分析发现的NFC标签,适当
 * 地对数据进行分类,并启动一个应用程序。在分类的数据中,要处理扫描NFC标签 的应用程序可以声明一个 intent filter来处理数据请求。
 * */
public class ForegroundDispatch extends Activity {
    // NfcAdapter相当于一个NFC适配器,就如同电脑装了网络适配器才可以上网一样,手机上装有NfcAdapter才能使用和NFC相应功能。
    private NfcAdapter mAdapter;
    // pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。
    private PendingIntent mPendingIntent;
    // IntentFilter对象负责过滤掉组件无法响应和处理的Intent,只将自己关心的Intent接收进来进行处理。
    private IntentFilter[] mFilters;
    private String[][] mTechLists;
    private TextView mText;
    private int mCount = 0;

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.foreground_dispatch);
        mText = (TextView) findViewById(R.id.text);
        mText.setText("Scan a tag");
        // NFC前台调度第一步:获取NFC适配器
        // 大部分Android设备只支持一个NFC Adapter,所以一般直接调用getDefaultAapater来获取手机中的Adapter。
        mAdapter = NfcAdapter.getDefaultAdapter(this);

        // 创建一个 PendingIntent, Android系统就能在一个tag被检测到时定位到这个对象
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        // 创建一个IntentFilter来过滤action
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            // 添加过滤的数据类型
            ndef.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        mFilters = new IntentFilter[] { ndef, };

        // 建立NFC s
        mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
    }

    @Override
    public void onResume() {
        super.onResume();
        // NFC前台调度第二步:在onResume()方法中启动前台调度功能
        // 设定intentfilter和tech-list。如果两个都为null就代表优先接收任何形式的TAG
        // action。也就是说系统会主动发TAG intent。
        if (mAdapter != null)
            mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
                    mTechLists);
    }

    @Override
    public void onNewIntent(Intent intent) {
        // 在onNewIntent()用来处理intent数据
        mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);
    }

    @Override
    public void onPause() {
        super.onPause();
        // NFC前台调度第二步: 在Pause()方法中禁用NFC前台调用功能
        if (mAdapter != null)
            mAdapter.disableForegroundDispatch(this);
    }
}

不要忘记在清单文件中添加NFC权限:

  <uses-permission android:name="android.permission.NFC" />
时间: 2024-08-28 13:05:48

NFC官方文档学习笔记(一):NFC前台调度的相关文章

hadoop官方文档学习笔记(1)——resource manager HA

resource manager HA是hadoop自从2.4之后推出的功能,以Active/Standby的方式提供冗余,目的是为了消除单点失败的风险. 1.总体架构: 2.故障切换:有自动和手动两种形式. 手动:如果以手动形式切换,使用yarn haadmin命令首先将Active节点转为standby,再将standby节点转为active. 自动:RM有基于zookeeper的节点选举机制决定哪一个是活动节点.不需要像HDFS一样部署一个zkfc守护进程,因为RM内嵌了这样的功能. 做了

Less 官方文档学习笔记

LESS 是css的一种扩展,它的编辑器是基于node.js 的less.js,将less文件编译成css文件(可压缩). 其中的概念: 变量:定义变量来代替某个值,只能编译一次,本质是“常量”.例如: @color:#ffddee; body { background-color:@color; } 输出的结果为: body { background-color:#ffddee; } 变量是延时加载的,只有用到的时候才会加载.变量的查找顺序是从下到上,最近优先. 混合(Mixin): 在一个选

jenkins官方文档学习笔记 初识Jenkins

什么是jenkins? Jenkins是一个用来监控重复工作的受到嘉奖的应用,比如构建一个软件项目或者定时执行的任务. 在这些工作中,Jenkins主要专注与以下两项工作: 1,持续构建/测试软件项目,Jenkens提供简单易用的所谓的持续集成系统,让开发者把变动集成到项目中变的更简单,让用户获得一个新的构建.自动持续的集成提高了生产效率. 2,监控外部调用执行的工作,比如cron jobs和procmail jobs,即使这些功能运行在远程机器上.例如,定时任务中,你会定期的收到捕获输出的邮件

Spring 4 官方文档学习(十二)View技术

1.介绍 Spring 有很多优越的地方,其中一个就是将view技术与MVC框架的其他部分相隔离.例如,在JSP存在的情况下使用Groovy Markup Templates 还是使用Thymeleaf,仅仅是一个配置问题. 本章覆盖了主要的view技术,嗯嗯,可以与Spring结合的那些,并简明的说明了如何增加新的view技术. 本章假定你已经熟悉了Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图 -- 它覆盖了views如何耦合到MVC框架

Effective Go(官方文档)笔记

Effective Go(官方文档)笔记 自己主动局部变量提升(编译期完毕?):return &...; 内置函数: new/make copy, append delete range(这是keyword吧?由于后面没有()) array是值对象 slice:引用array 2维切片(略) map if seconds, ok := timezone[tz]; ok { ... func (f *File) Read(buf []byte) (n int, err error) { ... 注

TensorFlow官方文档入门笔记[一]

TensorFlow官方文档入门笔记[一] 张量 3 # a rank 0 tensor; this is a scalar with shape [] [1., 2., 3.] # a rank 1 tensor; this is a vector with shape [3] [[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3] [[[1., 2., 3.]], [[7., 8., 9.]]] #

Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图

接前面的Spring 4 官方文档学习(十一)Web MVC 框架,那篇太长,故另起一篇. 针对web应用的所有的MVC框架,都会提供一种呈现views的方式.Spring提供了view resolvers,可以让你在浏览器中render model,而不必绑定到某种特定的view技术上.开箱即用,例如,Spring可以让你使用JSPs.Velocity目标和XSLT views.See Chapter 23, View technologies for a discussion of how

根据ThinkPHP官方文档学习opensns框架

根据ThinkPHP官方文档学习opensns框架 1.解读Application下各个Controller文件夹下的作用 控制器类的命名方式是:控制器名(驼峰法,首字母大写)+Controller 控制器文件的命名方式是:类名+class.php(类文件后缀) namespace Weibo\Controller; ///这是系统的规范要求,表示当前类是weibo模块下的控制器类,与实际路径一致 use Think\Controller; //引入 Think\Controller 类库便于直

React官方文档学习记录(四)- 条件渲染

一点点记录,建议需要学习React的移步官方文档去学习. 在React中,你可以创建一个清晰(distinct)的组件来简要描述你现在需要的东西.然后,你只需要使用你应用中的state来渲染它们. React中的条件型渲染跟JavaScript中的条件运算符运行方式差不多.好像就是使用JavaScript中的if或者三元运算符创建元素来显示现在的状态,然后让React更新UI来匹配这些修改. 下面这个例子就是根据不同的isLoggedIn进行不同的欢迎. 1 2 3 4 5 6 7 8 9 10