码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-14 15:41:28      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

上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" />

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

标签:

原文地址:http://blog.csdn.net/true100/article/details/51908017

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!