Android NFC标签写入网址,感应到标签访问网页
生活随笔
收集整理的這篇文章主要介紹了
Android NFC标签写入网址,感应到标签访问网页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先在AndroidManifest.xml文件中添加如下配置
<!-- SDK版本至少為14 --> <uses-sdk android:minSdkVersion="14"/> <!-- 添加NFC權限 --> <uses-permission android:name="android.permission.NFC" /> <!-- 要求當前設備必須要有NFC芯片 --> <uses-feature android:name="android.hardware.nfc" android:required="true" />然后創建一個NFC寫標的類MainActivity
NFC主要相關類有NdefMessage、NdefRecord、Ndef、NdefFormatable,具體使用方法見如下代碼及注釋
package com.xindecoiot.nfcreadinfo;import android.app.PendingIntent; import android.content.Intent; import android.net.Uri; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.nfc.tech.NdefFormatable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast;public class MainActivity extends AppCompatActivity {private NfcAdapter mNfcAdapter;private PendingIntent mPendingIntent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overrideprotected void onStart() {super.onStart();mNfcAdapter = NfcAdapter.getDefaultAdapter(this);// 用于感應到NFC時啟動該Activity// 這里建議將處理NFC的子類的launchMode設置成singleTop模式,這樣感應到標簽時就會回調onNewIntent,而不會重復打開頁面mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);}/*** 獲得焦點,按鈕可以點擊*/@Overridepublic void onResume() {super.onResume();// 設置當該頁面處于前臺時,NFC標簽會直接交給該頁面處理if (mNfcAdapter != null) {mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);}}/*** 暫停Activity,界面獲取焦點,按鈕可以點擊*/@Overridepublic void onPause() {super.onPause();// 當頁面不可見時,NFC標簽不交給當前頁面處理if (mNfcAdapter != null) {mNfcAdapter.disableForegroundDispatch(this);}}// 將Activity的launchMode設置成singleTop,這樣當感應到NFC標簽時不會重復打開頁面,而是回調該方法@Overridepublic void onNewIntent(Intent intent) {Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);writeTag(detectedTag);}/*** 寫標* @param tag*/public void writeTag(Tag tag) {if (tag == null) {return;}// 根據網址創建待寫入的數據NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{NdefRecord.createUri(Uri.parse("https://www.himmy.cn"))});// 獲取內容字節大小int size = ndefMessage.toByteArray().length;try {// 獲取NedfNdef ndef = Ndef.get(tag);// 不為空表示該標簽為Nedf格式if (ndef != null) {ndef.connect();// 是否可寫if (!ndef.isWritable()) {Toast.makeText(this, "標簽不支持寫入", Toast.LENGTH_SHORT).show();return;}// 判斷寫入內容大小是否超出允許寫入的最大值if (ndef.getMaxSize() < size) {Toast.makeText(this, "寫入內容過大", Toast.LENGTH_SHORT).show();return;}// 寫入數據ndef.writeNdefMessage(ndefMessage);Toast.makeText(this, "寫入成功", Toast.LENGTH_SHORT).show();} else { // 標簽非Nedf格式的情況NdefFormatable format = NdefFormatable.get(tag);// 不為空表示該標簽允許格式化成Ndef格式if (format != null) {format.connect();// 格式化并寫入Nedf內容format.format(ndefMessage);Toast.makeText(this, "寫入成功", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "標簽不支持Nedf格式", Toast.LENGTH_SHORT).show();}}} catch (Exception e) {}} }最后記得把MainActivity的啟動模式設置成singleTop
<activityandroid:name=".MainActivity"android:launchMode="singleTop"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter> </activity>測試
?
總結
以上是生活随笔為你收集整理的Android NFC标签写入网址,感应到标签访问网页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 容联云通讯—+springboot
- 下一篇: Codeforces Round #59