亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)讀取NFC卡卡號(hào)示例

 更新時(shí)間:2017年01月05日 09:09:44   作者:I_Gisvity  
本篇文章主要介紹了Android實(shí)現(xiàn)讀取NFC卡卡號(hào)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Android實(shí)現(xiàn)讀取NFC卡卡號(hào)示例,具體如下:

1.權(quán)限

  <uses-permission android:name="android.permission.NFC" />
    <uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

2.注冊(cè)(靜態(tài))

      <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED" />
        <data android:mimeType="text/plain" />
      </intent-filter>

3.Activity

初始化

    //初始化NfcAdapter
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        // 初始化PendingIntent,當(dāng)有NFC設(shè)備連接上的時(shí)候,就交給當(dāng)前Activity處理
    pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

啟動(dòng)

  @Override
  protected void onResume() {
    super.onResume();
    mNfcAdapter.enableForegroundDispatch(this, pi, null, null); //啟動(dòng)
  }

獲取數(shù)據(jù)

  @Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // 當(dāng)前app正在前端界面運(yùn)行,這個(gè)時(shí)候有intent發(fā)送過(guò)來(lái),那么系統(tǒng)就會(huì)調(diào)用onNewIntent回調(diào)方法,將intent傳送過(guò)來(lái)
    // 我們只需要在這里檢驗(yàn)這個(gè)intent是否是NFC相關(guān)的intent,如果是,就調(diào)用處理方法
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
      processIntent(intent);
    }
  }

解析

  /**
   * Parses the NDEF Message from the intent and prints to the TextView
   */
  private void processIntent(Intent intent) {
    //取出封裝在intent中的TAG
    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    String CardId =ByteArrayToHexString(tagFromIntent.getId());
  }
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;
  }

4.完整參考

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="cn.com.jslh.zjcdprogrect">

  <uses-permission android:name="android.permission.NFC" />
  <uses-permission android:name="android.permission.INTERNET" />

  <uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

  <application
    android:name=".common.MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".LoginActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".saoka.WorkActivity">
      <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED" />
        <data android:mimeType="text/plain" />
      </intent-filter>
      <!--<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />-->
    </activity>
  </application>

</manifest>

package cn.com.jslh.zjcdprogrect.saoka;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import cn.com.jslh.zjcdprogrect.R;

public class WorkActivity extends AppCompatActivity {

  private NfcAdapter mNfcAdapter;
  private PendingIntent pi;
  private IntentFilter tagDetected;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_work);

    //初始化NfcAdapter
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    //初始化PendingIntent
    // 初始化PendingIntent,當(dāng)有NFC設(shè)備連接上的時(shí)候,就交給當(dāng)前Activity處理
    pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    // 新建IntentFilter,使用的是第二種的過(guò)濾機(jī)制
//    tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
//    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
  }

  @Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // 當(dāng)前app正在前端界面運(yùn)行,這個(gè)時(shí)候有intent發(fā)送過(guò)來(lái),那么系統(tǒng)就會(huì)調(diào)用onNewIntent回調(diào)方法,將intent傳送過(guò)來(lái)
    // 我們只需要在這里檢驗(yàn)這個(gè)intent是否是NFC相關(guān)的intent,如果是,就調(diào)用處理方法
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
      processIntent(intent);
    }
  }

  @Override
  protected void onResume() {
    super.onResume();
    mNfcAdapter.enableForegroundDispatch(this, pi, null, null);
  }

  /**
   * Parses the NDEF Message from the intent and prints to the TextView
   */
  private void processIntent(Intent intent) {
    //取出封裝在intent中的TAG
    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    String CardId =ByteArrayToHexString(tagFromIntent.getId());
  }

  public static void startActivity(Context context){
    Intent intent = new Intent();
    intent.setClass(context,WorkActivity.class);
    context.startActivity(intent);
  }

  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;
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于Android中ListView嵌套GridView的問(wèn)題

    關(guān)于Android中ListView嵌套GridView的問(wèn)題

    在Android開發(fā)的過(guò)程中可能需要用到listview嵌套gridview的場(chǎng)景,但是在嵌套過(guò)程中也許會(huì)遇到問(wèn)題,我們下面一起來(lái)看看是什么問(wèn)題以及如何解決。
    2016-08-08
  • Android開發(fā)之動(dòng)畫實(shí)現(xiàn)方法

    Android開發(fā)之動(dòng)畫實(shí)現(xiàn)方法

    這篇文章主要介紹了Android開發(fā)之動(dòng)畫實(shí)現(xiàn)方法,實(shí)例分析了Android中動(dòng)畫的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05
  • Android 開發(fā)中使用Linux Shell實(shí)例詳解

    Android 開發(fā)中使用Linux Shell實(shí)例詳解

    這篇文章主要介紹了Android 開發(fā)中使用Linux Shell實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • android中SQLite使用及特點(diǎn)

    android中SQLite使用及特點(diǎn)

    SQLite是一個(gè)輕量級(jí)數(shù)據(jù)庫(kù),它設(shè)計(jì)目標(biāo)是嵌入式的,而且占用資源非常低,本文重點(diǎn)給大家介紹android中SQLite使用及特點(diǎn),感興趣的朋友跟隨小編一起看看吧
    2021-04-04
  • Android開發(fā)之自定義CheckBox

    Android開發(fā)之自定義CheckBox

    本文通過(guò)實(shí)例代碼演示如何在Android中如何自定義CheckBox,實(shí)現(xiàn)的效果很好,有需要的可以參考借鑒。
    2016-08-08
  • Android ToolBar控件詳解及實(shí)例

    Android ToolBar控件詳解及實(shí)例

    這篇文章主要介紹了 Android ToolBar控件詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android共享元素動(dòng)畫效果顯示問(wèn)題解決

    Android共享元素動(dòng)畫效果顯示問(wèn)題解決

    什么是共享元素呢?可以理解為當(dāng)頁(yè)面跳轉(zhuǎn)是,看起來(lái)一個(gè)View屬于界面A又屬于界面B,下面這篇文章主要給大家介紹了關(guān)于Android共享元素動(dòng)畫效果顯示問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • Flutter實(shí)現(xiàn)手勢(shì)識(shí)別功能詳解方法

    Flutter實(shí)現(xiàn)手勢(shì)識(shí)別功能詳解方法

    在Flutter中使用GestureDetector可以來(lái)完成對(duì)手勢(shì)的識(shí)別,包括長(zhǎng)按、滑動(dòng)、雙擊等手勢(shì),這篇文章主要介紹了Flutter實(shí)現(xiàn)手勢(shì)識(shí)別功能
    2022-11-11
  • Android自定義短信驗(yàn)證碼組件

    Android自定義短信驗(yàn)證碼組件

    這篇文章主要為大家詳細(xì)介紹了Android自定義短信驗(yàn)證碼組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Android Map新用法:MapFragment應(yīng)用介紹

    Android Map新用法:MapFragment應(yīng)用介紹

    MapView ,MapActivity 這種的局限在于,必須要繼承MapActivity,否則無(wú)法使用MapView,但是,MapFragment 這種的局限在于,必須要安裝Google Play Service ,也就是說(shuō)必須是原生rom。而且sdk要在12以上
    2013-01-01

最新評(píng)論