最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • Flutter Map常用操作方法總結(jié)

    Flutter Map常用操作方法總結(jié)

    Flutter 中的 Map 是一種鍵值對(duì)的集合,可以存儲(chǔ)任意類(lèi)型的數(shù)據(jù),并且可以通過(guò)鍵來(lái)訪問(wèn)和操作對(duì)應(yīng)的值,下面我們就來(lái)學(xué)習(xí)一下Flutter Map的常用操作方法吧
    2023-11-11
  • Android?Studio中如何修改APP圖標(biāo)和APP名稱(chēng)

    Android?Studio中如何修改APP圖標(biāo)和APP名稱(chēng)

    這篇文章主要介紹了Android?Studio中如何修改APP圖標(biāo)和APP名稱(chēng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Android自定義控件之水平圓點(diǎn)加載進(jìn)度條

    Android自定義控件之水平圓點(diǎn)加載進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android自定義控件之水平圓點(diǎn)加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Android自定義標(biāo)尺滑動(dòng)選擇值效果

    Android自定義標(biāo)尺滑動(dòng)選擇值效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義標(biāo)尺滑動(dòng)選擇值效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Android WebView 不支持 H5 input type=

    Android WebView 不支持 H5 input type="file" 解決方法

    這篇文章主要介紹了Android WebView 不支持 H5 input type="file" 解決方法,需要的朋友可以參考下
    2017-06-06
  • 淺談Android程序與JavaScript腳本的交互

    淺談Android程序與JavaScript腳本的交互

    這篇文章主要介紹了Android程序與JavaScript的腳本交互,利用js開(kāi)發(fā)安卓應(yīng)用也是近來(lái)的熱門(mén)話(huà)題,特別是隨著Facebook的React Native的開(kāi)源及對(duì)Android的官方支持,需要的朋友可以參考下
    2016-04-04
  • Android?ViewPager?+?Fragment實(shí)現(xiàn)滑動(dòng)頁(yè)面效果

    Android?ViewPager?+?Fragment實(shí)現(xiàn)滑動(dòng)頁(yè)面效果

    本文通過(guò)實(shí)例代碼較詳細(xì)的給大家介紹了Android?ViewPager?+?Fragment實(shí)現(xiàn)滑動(dòng)頁(yè)面效果,需要的朋友可以參考下
    2018-06-06
  • Android列表動(dòng)圖展示的實(shí)現(xiàn)策略

    Android列表動(dòng)圖展示的實(shí)現(xiàn)策略

    這篇文章主要給大家介紹了關(guān)于Android列表動(dòng)圖展示的實(shí)現(xiàn)策略的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Android結(jié)合x(chóng)ml實(shí)現(xiàn)幀動(dòng)畫(huà)

    Android結(jié)合x(chóng)ml實(shí)現(xiàn)幀動(dòng)畫(huà)

    將一組動(dòng)作相近的圖片組合在一起,然后按照一定的時(shí)間來(lái)播放,就會(huì)形成一個(gè)動(dòng)畫(huà),我們可以稱(chēng)之為幀動(dòng)畫(huà)。在 Android 中可通過(guò)結(jié)合 xml 的方式來(lái)輕松實(shí)現(xiàn)。
    2021-05-05
  • Android對(duì)話(huà)框使用方法詳解

    Android對(duì)話(huà)框使用方法詳解

    這篇文章主要介紹了Android對(duì)話(huà)框使用方法,包括提示對(duì)話(huà)框、單選對(duì)話(huà)框、復(fù)選對(duì)話(huà)框、列表對(duì)話(huà)框等,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09

最新評(píng)論

珲春市| 平遥县| 麻江县| 射洪县| 土默特左旗| 台前县| 成安县| 大余县| 突泉县| 定陶县| 鲁甸县| 汶上县| 乐安县| SHOW| 海安县| 甘德县| 视频| 南乐县| 银川市| 留坝县| 巴彦淖尔市| 西丰县| 平罗县| 浙江省| 江津市| 屏东市| 林口县| 沁阳市| 富民县| 建阳市| 宁远县| 会宁县| 霞浦县| 四川省| 甘泉县| 淮南市| 小金县| 陇西县| 东山县| 余干县| 丽水市|