8、NFC技术:让Android自动打开网页
16lz
2021-01-24
创建封装Uri的NdefRecord public NdefRecord createUri(String uriString);
public NdefRecord createUri(Uri uri);
范例:自动打开网页1 import android.app.Activity; 2 import android.app.PendingIntent; 3 import android.content.Intent; 4 import android.net.Uri; 5 import android.nfc.NdefMessage; 6 import android.nfc.NdefRecord; 7 import android.nfc.NfcAdapter; 8 import android.nfc.Tag; 9 import android.nfc.tech.Ndef;10 import android.nfc.tech.NdefFormatable;11 import android.os.Bundle;12 import android.widget.Toast;13 14 public class AutoOpenUriActivity extends Activity {15 private NfcAdapter nfcAdapter;16 private PendingIntent pendingIntent;17 18 @Override19 public void onCreate(Bundle savedInstanceState) {20 super.onCreate(savedInstanceState);21 22 setContentView(R.layout.activity_auto_open_uri);23 nfcAdapter = NfcAdapter.getDefaultAdapter(this);24 pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,25 getClass()), 0);26 27 }28 29 @Override30 public void onResume() {31 super.onResume();32 if (nfcAdapter != null)33 nfcAdapter34 .enableForegroundDispatch(this, pendingIntent, null, null);35 }36 37 @Override38 public void onNewIntent(Intent intent) {39 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);40 writeNFCTag(detectedTag);41 }42 43 @Override44 public void onPause() {45 super.onPause();46 if (nfcAdapter != null)47 nfcAdapter.disableForegroundDispatch(this);48 49 }50 51 public void writeNFCTag(Tag tag) {52 if (tag == null) {53 return;54 }55 NdefMessage ndefMessage = new NdefMessage(56 new NdefRecord[] { NdefRecord.createUri(Uri57 .parse("http://www.baidu.com")) });58 int size = ndefMessage.toByteArray().length;59 try {60 Ndef ndef = Ndef.get(tag);61 62 if (ndef != null) { // 证明标签是否是NDEF格式63 ndef.connect();64 if (!ndef.isWritable()) {65 return;66 }67 if (ndef.getMaxSize() < size) {68 return;69 }70 ndef.writeNdefMessage(ndefMessage);71 Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();72 } else {73 // 格式化成NDEF格式。74 NdefFormatable format = NdefFormatable.get(tag);75 if (format != null) {76 format.connect();77 format.format(ndefMessage);78 Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();79 80 } else {81 Toast.makeText(this, "formating is failed",82 Toast.LENGTH_LONG).show();83 }84 85 }86 87 } catch (Exception e) {88 // TODO: handle exception89 }90 91 }92 93 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content"10 android:layout_marginBottom="5dp"11 android:text="请将NFC标签或贴纸靠近手机背面"12 android:textSize="16sp" />13 14 <ImageView15 android:layout_width="match_parent"16 android:layout_height="match_parent"17 android:layout_margin="10dp"18 android:src="@drawable/read_nfc_tag" />19 20 </LinearLayout>
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="cn.eoe.open.uri" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="15" 8 android:targetSdkVersion="15" /> 9 10 <uses-permission android:name="android.permission.NFC" />11 12 <application13 android:icon="@drawable/ic_launcher"14 android:label="@string/app_name"15 android:theme="@style/AppTheme" >16 <activity17 android:name="AutoOpenUriActivity"18 android:label="@string/title_activity_auto_run_application"19 android:launchMode="singleTop"20 android:screenOrientation="portrait" >21 <intent-filter>22 <action android:name="android.intent.action.MAIN" />23 24 <category android:name="android.intent.category.LAUNCHER" />25 </intent-filter>26 </activity>27 28 </application>29 30 </manifest>
更多相关文章
- Android保留两位小数方法
- android的TextView使用Html(图片显示)格式
- Android(安卓)下使用 JSON 实现 HTTP 请求,外加几个示例!
- Android字符串格式化
- Android(安卓)layer-list 中图片被拉伸问题
- Android日期时间格式国际化
- Android(安卓)studio编译警告:请使用 -Xlint:deprecation 重新编
- 使用MediaRecorder录制音频
- android/java中短信pdu编码