8、NFC技术:让Android自动打开网页
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自动打开网页的更多相关文章
- android之打开网页
首先改写strings.xml文件 代码如下: <resources> <string name="app_name">Intent应用</strin ...
- inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效
inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效 inno setup 安装卸载时检测程序是佛正在运行卸载完成后自动打开网页-代码无效 --------------------- ...
- [安卓] 15、用NFC解锁手机并自动打开应用
最近接到一个项目:将手机放到一个带有NFC卡的底座上手机会自动解锁,然后打开相应的应用 本人用:杭州公交通用卡做为NFC卡+Coolpad手机进行试验 效果如下: 1.手机本身带有图案锁,输对图案才能 ...
- vue npm start 自动打开网页
打开config/index.js,设置: autoOpenBrowser: true,(默认是false,把false改为true即可)
- Mac 下安装python3.7 + pip 利用 chrome + chromedriver + selenium 自动打开网页并自动点击访问指定页面
1.安装python3.7https://www.python.org/downloads/release/python-370/选择了这个版本,直接默认下一步 2.安装pipcurl https:/ ...
- Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord
Android NFC技术(三)--初次开发Android NFC你须知道NdefMessage和NdefRecord 这最近也是有好多天没写博客了,除了到处张罗着搬家之外,依旧还是许许多多的琐事阻碍 ...
- 6、Android中的NFC技术
Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...
- Python实用案例,Python脚本,Python实现自动监测Github项目并打开网页
往期回顾 Python实现文件自动归类 前言: 今天我们就利用Python脚本实现Github项目的更新,提醒方式是邮箱.直接开整~ 项目地址: https://github.com/kenwoodj ...
- 7、NFC技术:让Android自动运行程序
用于描述NDEF格式数据的两个重要的类 NdefMessage:描述NDEF格式的信息 NdefRecord:描述NDEF信息的一个信息段 NdefMessage和NdefRecord是Androi ...
随机推荐
- Java:字符串类String的功能介绍
在java中,字符串是一个比较常用的类,因为代码中基本上处理的很多数据都是字符串类型的,因此,掌握字符串类的具体用法显得很重要了. 它的主要功能有如下几种:获取.判断.转换.替换.切割.字串的获取.大 ...
- 用rbenv给整个系统安装ruby(所有用户都可用)
rbenv非常好用,但有一个小问题,每个用户都需要单独安装和设置版本. 下面安装步骤可以安装在/usr/local/rbenv下,所有用户都可以共享rbenv. #rbenv安装(/usr/loc ...
- Socket基础编程
地址结构sockaddr_in 其中包含:IP地址,端口号,协议族推荐使用sockaddr_in,而不建议使用sockaddrsockaddr_in与sockaddr是等价的,但sockaddr_in ...
- dojo 二 AMD模块
可参考官方教程:http://dojotoolkit.org/documentation/tutorials/1.7/hello_dojo/教程里主要定义了两个方法,setText设置文本内容和res ...
- env1
给新手参考的Cadence Allegro快捷键 原文链接:http://www.eda365.com/thread-30935-1-1.html 发上我的快捷键给新手参考,我也是在LULU给我的EN ...
- hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. #include <cstdio> #inclu ...
- 第六讲(二) Hibernate HQL查询
HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此Hibe ...
- eclipse中字母大小写转换快捷键
ctrl+shift+x 转为大写 ctrl+shift+y 转为小写
- jQuery中filter(),not(),split()的用法
filter(),not(): <script type="text/javascript"> $(document).ready(function() { //输出 ...
- (转载)C语言 数组与指针的区别
1) 字符串指针变量是个变量,指向字符串的首地址:而字符串数组名是个常量,为字符串数组第一个元素的地址: 2)字符串指针变量可以赋值,而字符串数组名不能赋值:对于字符数组只能对各个元素赋值,不能用以下 ...