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的数据库中 主要的两张表 读取联系人 读取联系人需要知道联系人内容提供者的地址,以及对应的操作对象.一般情况下操作对象是的命名方式和表明是一致的. 布局文 ...
随机推荐
- BZOJ 1261: [SCOI2006]zh_tree( 区间dp )
dp(l, r)表示[l, r]这段作为一棵树的最小访问代价. 对于dp(l, r), 我们枚举它的根x, 则dp(l, r) = min(dp(l, x-1)+dp(x+1, r)+C*fx) + ...
- BZOJ 1820: [JSOI2010]Express Service 快递服务( dp )
dp(i,j,k)表示在处理第i个业务, 另外2个在j,k处. 第一维可以滚动... --------------------------------------------------------- ...
- js 选择器
a>b 获取a下面的直接子元素
- python类继承
面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制.继承完全可以理解成类之间的 类型和子类型 关系. 假设你想要写一个程序来记录学校之中的教师和学生情况.他们有一些 ...
- RMAN多种备份脚本分享
1.相关参数介绍: 命令行参数 描述 TARGET 为目标数据库定义的一个连接字符串,当连接到一个目标数据库时,该连续是SYSDBA连接.该用户拥有启动和关闭数据库的权利,必须属于OSDBA组,必须建 ...
- poj 3176 Cow Bowling(区间dp)
题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...
- Java数据类型BooleanDemo
- vim下使用YouCompleteMe实现代码提示、补全以及跳转设置
配置YouCompleteMe 1. 安装vundle vundle是一个管理vim插件的工具,使用vundle安装YouCompleteMe比较方便. 按照作者在https://github.com ...
- 10994 - Simple Addition(规律)
Problem E Simple Addition Input: Standard Input Output: Standard Output Let’s define a simple recurs ...
- 网络编程TCP协议-聊天室
网络编程TCP协议-聊天室(客户端与服务端的交互); <span style="font-size:18px;">1.客户端发数据到服务端.</span> ...