国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現讀取NFC卡卡號示例

2019-12-12 04:07:41
字體:
來源:轉載
供稿:網友

Android實現讀取NFC卡卡號示例,具體如下:

1.權限

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

2.注冊(靜態)

      <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,當有NFC設備連接上的時候,就交給當前Activity處理    pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

啟動

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

獲取數據

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

解析

  /**   * 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,當有NFC設備連接上的時候,就交給當前Activity處理    pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);    // 新建IntentFilter,使用的是第二種的過濾機制//    tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);//    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);  }  @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);  }  /**   * 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;  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成都市| 宜川县| 西畴县| 弥勒县| 尤溪县| 南和县| 望都县| 巴里| 航空| 玉屏| 长子县| 镇江市| 金阳县| 昔阳县| 洛隆县| 红安县| 六安市| 道真| 靖江市| 道真| 洞头县| 屏东市| 邵东县| 秦安县| 洮南市| 西丰县| 德州市| 永福县| 江安县| 托里县| 禄劝| 民勤县| 梁平县| 汤原县| 黑龙江省| 石台县| 龙岩市| 布拖县| 锦屏县| 沧源| 长宁县|