创建封装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>

更多相关文章

  1. Android保留两位小数方法
  2. android的TextView使用Html(图片显示)格式
  3. Android(安卓)下使用 JSON 实现 HTTP 请求,外加几个示例!
  4. Android字符串格式化
  5. Android(安卓)layer-list 中图片被拉伸问题
  6. Android日期时间格式国际化
  7. Android(安卓)studio编译警告:请使用 -Xlint:deprecation 重新编
  8. 使用MediaRecorder录制音频
  9. android/java中短信pdu编码

随机推荐

  1. android 基于ffmpeg将视频采集的nv21转h2
  2. android手电筒
  3. android input子系统--InputReader Event
  4. 修改Android中strings.xml文件
  5. Dagger2+Retrofit+RxJava
  6. Android中framework层下添加aidl编译说程
  7. Android报表控件achartengine介绍(二)
  8. Android(安卓)Studio4如何在查看低版本AP
  9. Android用户手势检测
  10. Android中IntentService和HandlerThread