创建封装Uri的NdefRecord
 public  NdefRecord  createUri(String  uriString); 

public  NdefRecord  createUri(Uri  uri); 

范例:自动打开网页
 import android.app.Activity;
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.widget.Toast; public class AutoOpenUriActivity extends Activity {
private NfcAdapter nfcAdapter;
private PendingIntent pendingIntent; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_auto_open_uri);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()), 0); } @Override
public void onResume() {
super.onResume();
if (nfcAdapter != null)
nfcAdapter
.enableForegroundDispatch(this, pendingIntent, null, null);
} @Override
public void onNewIntent(Intent intent) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
writeNFCTag(detectedTag);
} @Override
public void onPause() {
super.onPause();
if (nfcAdapter != null)
nfcAdapter.disableForegroundDispatch(this); } public void writeNFCTag(Tag tag) {
if (tag == null) {
return;
}
NdefMessage ndefMessage = new NdefMessage(
new NdefRecord[] { NdefRecord.createUri(Uri
.parse("http://www.baidu.com")) });
int size = ndefMessage.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag); if (ndef != null) { // 证明标签是否是NDEF格式
ndef.connect();
if (!ndef.isWritable()) {
return;
}
if (ndef.getMaxSize() < size) {
return;
}
ndef.writeNdefMessage(ndefMessage);
Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();
} else {
// 格式化成NDEF格式。
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
format.connect();
format.format(ndefMessage);
Toast.makeText(this, "ok", Toast.LENGTH_LONG).show(); } else {
Toast.makeText(this, "formating is failed",
Toast.LENGTH_LONG).show();
} } } catch (Exception e) {
// TODO: handle exception
} } }
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="请将NFC标签或贴纸靠近手机背面"
android:textSize="16sp" /> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:src="@drawable/read_nfc_tag" /> </LinearLayout>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.eoe.open.uri"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.NFC" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="AutoOpenUriActivity"
android:label="@string/title_activity_auto_run_application"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </application> </manifest>

8、NFC技术:让Android自动打开网页的更多相关文章

  1. android之打开网页

    首先改写strings.xml文件 代码如下: <resources> <string name="app_name">Intent应用</strin ...

  2. inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效

    inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效 inno setup 安装卸载时检测程序是佛正在运行卸载完成后自动打开网页-代码无效 --------------------- ...

  3. [安卓] 15、用NFC解锁手机并自动打开应用

    最近接到一个项目:将手机放到一个带有NFC卡的底座上手机会自动解锁,然后打开相应的应用 本人用:杭州公交通用卡做为NFC卡+Coolpad手机进行试验 效果如下: 1.手机本身带有图案锁,输对图案才能 ...

  4. vue npm start 自动打开网页

    打开config/index.js,设置: autoOpenBrowser: true,(默认是false,把false改为true即可)

  5. Mac 下安装python3.7 + pip 利用 chrome + chromedriver + selenium 自动打开网页并自动点击访问指定页面

    1.安装python3.7https://www.python.org/downloads/release/python-370/选择了这个版本,直接默认下一步 2.安装pipcurl https:/ ...

  6. Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord

    Android NFC技术(三)--初次开发Android NFC你须知道NdefMessage和NdefRecord 这最近也是有好多天没写博客了,除了到处张罗着搬家之外,依旧还是许许多多的琐事阻碍 ...

  7. 6、Android中的NFC技术

    Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...

  8. Python实用案例,Python脚本,Python实现自动监测Github项目并打开网页

    往期回顾 Python实现文件自动归类 前言: 今天我们就利用Python脚本实现Github项目的更新,提醒方式是邮箱.直接开整~ 项目地址: https://github.com/kenwoodj ...

  9. 7、NFC技术:让Android自动运行程序

    用于描述NDEF格式数据的两个重要的类 NdefMessage:描述NDEF格式的信息 NdefRecord:描述NDEF信息的一个信息段  NdefMessage和NdefRecord是Androi ...

随机推荐

  1. 自绘CListCtrl类,重载虚函数DrawItem

    //自绘CListCtrl类,重载虚函数DrawItem void CNewListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TOD ...

  2. IDL基础

    先列后行 arr=indgen(3,4) SIZE(arr,/dimensions) print ,where(arr gt7) print,arr[where(arr gt 7)] print,wh ...

  3. Sails 自定义 model 方法

    Sails 自定义 model 方法 在 Sails 中 model 提供了一些原生的静态方法,如 .create(), .update(), .destroy(), .find(), 等. 在实际业 ...

  4. Redhat6下安装QEMU

    Redhat6下安装QEMU 1.下载QEUM:http://wiki.qemu.org/Download 2.解压qemu-1.6.1.tar.bz2到/tmp目录(也可以是其他目录)下,并进入解压 ...

  5. Object窥探

    /* * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...

  6. Android内存管理(2)HUNTING YOUR LEAKS: MEMORY MANAGEMENT IN ANDROID PART 2

    from: http://www.raizlabs.com/dev/2014/04/hunting-your-leaks-memory-management-in-android-part-2-of- ...

  7. 2-Medium下的MultipleCommandAssembly

    1.首先创建一个控制台项目 2.添加引用[红色的都是将项目添加为引用,其中蓝色的log4net是直接从源代码下的Reference文件夹下引用的dll] 3.写代码 1)首先将config配置好[ht ...

  8. 头文件中的#ifndef/#define/#endif 的作用

    在一个大的软件工程里面,可能会有多个文件同时包含一个头文件,当这些文件编译链接成一个可执行文件时,就会出现大量重定义的错误.在头文件中实用#ifndef #define #endif能避免头文件的重定 ...

  9. 手机号段、ip地址归属地大全,最新手机号段归属地,IP地址归属地数据库

    百事通:http://www.114best.com/dh/114.aspx?w=17097232323,联通识别为电信的,1349错 二三四五:http://tools.2345.com/frame ...

  10. STL笔记(5)条款49:学习破解有关STL的编译器诊断信息

    STL笔记(5)条款49:学习破解有关STL的编译器诊断信息 条款49:学习破解有关STL的编译器诊断信息 用一个特定的大小定义一个vector是完全合法的, vector<int> v( ...