Android NFC传输联系人VCF
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Profile;
import android.util.Log; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; /**
* This class implements sharing the currently displayed
* contact to another device using NFC. NFC sharing is only
* enabled when the activity is in the foreground and resumed.
* When an NFC link is established, {@link #createMessage}
* will be called to create the data to be sent over the link,
* which is a vCard in this case.
*/
public class NfcHandler implements NfcAdapter.CreateNdefMessageCallback { private static final String TAG = "ContactNfcHandler";
private static final String PROFILE_LOOKUP_KEY = "profile";
private final Context mContext;
private final Uri mContactUri; /* Register NFC handler. This should be called in activities' onCreate(), or similar methods */
public static void register(Activity activity, Uri contactUri) {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity.getApplicationContext());
if (adapter == null) {
return; // NFC not available on this device
}
adapter.setNdefPushMessageCallback(new NfcHandler(activity, contactUri), activity);
} public NfcHandler(Context context, Uri contactUri) {
mContext = context;
mContactUri = contactUri;
} @Override
public NdefMessage createNdefMessage(NfcEvent event) {
ContentResolver resolver = mContext.getContentResolver();
if (mContactUri != null) {
final String lookupKey = Uri.encode(mContactUri.getPathSegments().get(2));
final Uri shareUri;
// TODO find out where to get this constant from, or find another way
// of determining this.
if (lookupKey.equals(PROFILE_LOOKUP_KEY)) {
shareUri = Profile.CONTENT_VCARD_URI.buildUpon().
appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").
build();
} else {
shareUri = Contacts.CONTENT_VCARD_URI.buildUpon().
appendPath(lookupKey).
appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").
build();
}
ByteArrayOutputStream ndefBytes = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int r;
try {
InputStream vcardInputStream = resolver.openInputStream(shareUri);
while ((r = vcardInputStream.read(buffer)) > 0) {
ndefBytes.write(buffer, 0, r);
} NdefRecord record = NdefRecord.createMime("text/x-vcard", ndefBytes.toByteArray());
return new NdefMessage(record);
} catch (IOException e) {
Log.e(TAG, "IOException creating vcard.");
return null;
}
} else {
Log.w(TAG, "No contact URI to share.");
return null;
}
}
}
activity那边调用的代码:
NfcHandler.register(this, mLookupUri);
上面两段代码,来自Android源码里面的Contacts
work_space/packages/apps/Contacts/
Android NFC传输联系人VCF的更多相关文章
- Android NFC标签 开发深度解析 触碰的艺术
有几天没有更新博客了,不过本篇却准备了许久,希望能带给每一位开发者最简单高效的学习方式.废话到此为止,下面开始正文. NFC(Near Field Communication,近场通信)是一种数据传输 ...
- 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING
<Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th 2014 Email:skyseraph00@163.com 更多精彩请直接 ...
- 【转】Android NFC学习笔记
一:NFC的tag分发系统 如果想让android设备感应到NFC标签,你要保证两点 1:屏幕没有锁住 2:NFC功能已经在设置中打开 当系统检测到一个NFC标签的时候,他会自动去寻找最合适的acti ...
- Android NFC开发概述
NFC手机相比普通手机来说,有以下3个附加功能: 1.可以当成POS机来用,也就是“读取”模式 2.可以当成一张卡来刷,也就是NFC技术最核心的移动支付功能 3.可以像蓝牙.Wi-Fi一样做点 ...
- Android NFC开发(二)——Android世界里的NFC所具备的条件以及使用方法
Android NFC开发(二)--Android世界里的NFC所具备的条件以及使用方法 NFC的应用比较广泛,而且知识面也是比较广的,所以就多啰嗦了几句,我还还是得跟着官方文档:http://dev ...
- Android NFC开发(一)——初探NFC,了解当前前沿技术
Android NFC开发(一)--初探NFC,了解当前前沿技术 官方文档:http://developer.android.com/guide/topics/connectivity/nfc/ind ...
- android nfc中Ndef格式的读写
1. 在onCreate()中获取NfcAdapter对象: NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 2.在onNewI ...
- android nfc中MifareClassic格式的读写
Android支持的数据格式 数据格式的Intent filter AndroidManifest.xml文件中,要像向下列示例那样,在<activity>元素内的<meta-dat ...
- android之读取联系人信息
联系人信息被存放在一个contacts2.db的数据库中 主要的两张表 读取联系人 读取联系人需要知道联系人内容提供者的地址,以及对应的操作对象.一般情况下操作对象是的命名方式和表明是一致的. 布局文 ...
随机推荐
- C#利用委托跨线程更新UI数据
转:http://www.2cto.com/kf/201206/136587.html 在使用C#的过程中,难免会用到多线程,而用多线程之后,线程如何与界面交互则是一个非常头疼的问题.其实不仅仅是界面 ...
- ASP.net体系
- CentOS环境下R语言的安装和配置
最近在看数据统计和分析,想到了R语言,于是就着手在自己的CentOS环境下进行安装和配置.步骤如下: 1.前往R官网下载安装包. 2.解压压缩包:tar xvzf R-3.2.2.tar.gz 3.进 ...
- HBase性能测试
hbase org.apache.hadoop.hbase.PerformanceEvaluationUsage: java org.apache.hadoop.hbase.PerformanceEv ...
- jsp中forward和redirect的区别(转)
一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下: request.getRequestDispatcher("new.jsp").forward(reques ...
- php 判断是否登录
<?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action { public function _before_index(){ //做 ...
- Android 的平台碎片化问题
Android 的平台碎片化问题 看到篇不错的文章,转载过来. -------------------------------------- 与iOS开发相比,Android开发平添了不小的工作量,因 ...
- 深入探究VC —— 资源编译器rc.exe(3)
Windows应用程序中,图标.菜单.畏途.图标.工具条.对话框等是以资源的形式存在的.开发人员也可以自定义资源类型.如果一个程序使用了资源,那么它在构建时需要对资源进行编译.程序所使用的资源会在资源 ...
- Android apk逆向实战
简介 逆向Android apk其实是一个分析Android apk的一个过程,必须了解Android程序开发的流程.结构.语句分支.解密原理等等. 功能 破解一个注册验证程序(自写一个简单的注册验证 ...
- CentOS下安装两个或多个Tomcat7
链接地址:http://lcbk.net/tomcat/1407.html 首先安装JDK 安装之前检查下是否已经安装了openJDK,如果已安装,建议用yum remove 卸载掉. [root@b ...