1.获取权限

        

2.设置NFC活动页

                                             

3.Activity代码

    //NFC对象    private NfcAdapter mNfcAdapter;    private PendingIntent pi;

1.主方法中:

//获取默认的NFC控制器        //初始化NfcAdapter        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);        if (mNfcAdapter == null) {            Toast.makeText(AddPointActivity.this, "设备不支持NFC!", Toast.LENGTH_LONG).show();            finish();            return;        }        if (!mNfcAdapter.isEnabled()) {            Toast.makeText(AddPointActivity.this, "请在系统设置中先启用NFC功能!", Toast.LENGTH_LONG).show();            finish();            return;        }        //初始化PendingIntent        // 初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理        pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

2.方法

//获取数据    @Override    protected void onNewIntent(Intent intent) {        super.onNewIntent(intent);        // 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来        // 我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {            processIntent(intent);        }    }    //启动    @Override    protected void onResume() {        super.onResume();        mNfcAdapter.enableForegroundDispatch(this, pi, null, null);    }    //解析    private void processIntent(Intent intent) {        //取出封装在intent中的TAG        Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);        String CardId =ByteArrayToHexString(tagFromIntent.getId());        Toast.makeText(AddPointActivity.this, CardId, Toast.LENGTH_LONG).show();    }    //转为16进制字符串    private String ByteArrayToHexString(byte[] inarray) {        int i, j, in;        String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",                "B", "C", "D", "E", "F" };        String out = "";        for (j = 0; j < inarray.length; ++j) {            in = (int) inarray[j] & 0xff;            i = (in >> 4) & 0x0f;            out += hex[i];            i = in & 0x0f;            out += hex[i];        }        return out;    }

运行结果:

更多相关文章

  1. android去除标题栏-------全屏运行AND 无标题栏 总结
  2. Android(安卓)源码解析 - ScrollView
  3. 系统命令行运行 android app程序
  4. Android(安卓): 自己写个HTTP框架
  5. [置顶] Android(安卓)系统应用调用,intent 的使用方法总结
  6. android遇到的BUG
  7. Android(安卓)JNI入门第五篇——基本数据类型使用
  8. Android(安卓)Eventbus控件发送与接收
  9. Android(安卓)中 Bitmap 和 Drawable 相互转换的方法

随机推荐

  1. Android: HowTo设置app不被系统kill掉
  2. android SDK Manager 无法更新的解决办法
  3. 【Android Demo】悬浮窗体实现
  4. Android(安卓)在工作线程(非主线程)更改UI
  5. Android(安卓)Activity 生命周期
  6. Android(安卓)App程序结构
  7. Android网络开发详解
  8. android 程序检查当前应用是否在运行
  9. Android(安卓)WebView 禁止输入
  10. android日志工具LogCat的使用