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的更多相关文章

  1. Android NFC标签 开发深度解析 触碰的艺术

    有几天没有更新博客了,不过本篇却准备了许久,希望能带给每一位开发者最简单高效的学习方式.废话到此为止,下面开始正文. NFC(Near Field Communication,近场通信)是一种数据传输 ...

  2. 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING

    <Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th  2014 Email:skyseraph00@163.com 更多精彩请直接 ...

  3. 【转】Android NFC学习笔记

    一:NFC的tag分发系统 如果想让android设备感应到NFC标签,你要保证两点 1:屏幕没有锁住 2:NFC功能已经在设置中打开 当系统检测到一个NFC标签的时候,他会自动去寻找最合适的acti ...

  4. Android NFC开发概述

    NFC手机相比普通手机来说,有以下3个附加功能:  1.可以当成POS机来用,也就是“读取”模式   2.可以当成一张卡来刷,也就是NFC技术最核心的移动支付功能  3.可以像蓝牙.Wi-Fi一样做点 ...

  5. Android NFC开发(二)——Android世界里的NFC所具备的条件以及使用方法

    Android NFC开发(二)--Android世界里的NFC所具备的条件以及使用方法 NFC的应用比较广泛,而且知识面也是比较广的,所以就多啰嗦了几句,我还还是得跟着官方文档:http://dev ...

  6. Android NFC开发(一)——初探NFC,了解当前前沿技术

    Android NFC开发(一)--初探NFC,了解当前前沿技术 官方文档:http://developer.android.com/guide/topics/connectivity/nfc/ind ...

  7. android nfc中Ndef格式的读写

    1. 在onCreate()中获取NfcAdapter对象: NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 2.在onNewI ...

  8. android nfc中MifareClassic格式的读写

    Android支持的数据格式 数据格式的Intent filter AndroidManifest.xml文件中,要像向下列示例那样,在<activity>元素内的<meta-dat ...

  9. android之读取联系人信息

    联系人信息被存放在一个contacts2.db的数据库中 主要的两张表 读取联系人 读取联系人需要知道联系人内容提供者的地址,以及对应的操作对象.一般情况下操作对象是的命名方式和表明是一致的. 布局文 ...

随机推荐

  1. java线程之停止线程

         在Java中有以下3种方法可以终止一个正在运行的线程:      1.使用退出标志,是线程正常退出,也就是run方法完成后线程终止.      2.使用stop方法强制终止线程,但不推荐使用 ...

  2. java反射机制入门04

    需要jxl.jar package com.rainmer.main; import java.io.File; import java.io.IOException; import java.uti ...

  3. Phases of translation

    Phases of translation--翻译阶段 The C++ source file is processed by the compiler as if the following pha ...

  4. django 开发简易博客(二)

    这一节我们来了解模板和视图.URL的使用. 一.使用模板 在blog目录中新建templates文件夹,在templates文件夹下新建base.html文件.目录结构如下 templates/ ba ...

  5. python 字典有序无序及查找效率,hash表

    刚学python的时候认为字典是无序,通过多次插入,如di = {}, 多次di['testkey']='testvalue' 这样测试来证明无序的.后来接触到了字典查找效率这个东西,查了一下,原来字 ...

  6. NET Core,跨平台的轻量级RPC

    NET Core,跨平台的轻量级RPC:Rabbit.Rpc 特性一览 Apache License 2.0协议开源 支持客户端负载均衡(提供了轮询.随机算法的实现) 支持ZooKeeper和文件共享 ...

  7. Qt 技巧:设置在 debug 路径下直接运行可执行文件

    Qt 编译的时候默认会使用影子构建,这时在工作路径下会生成一个目录:xxx-build-desktop-xxx. 如果编译通过,在该目录下会生成一个可执行文件,双击之,但不能运行,提示是缺少某个动态库 ...

  8. 用macports装了一份openssl

    我已经用macports装了一份openssl,然后自己又编译了一份openssl....第三方给Mac出的一个类似BSD Ports的一个软件包管理工具装的话只需要sudo port install ...

  9. 啊华北哦好咕~~(╯﹏╰)b

    http://v.qq.com/boke/page/c/h/0/c01173tzeh0.html http://v.qq.com/boke/page/r/7/x/r0117l07r7x.html ht ...

  10. 【LeetCode】【Python题解】Single Number &amp; Maximum Depth of Binary Tree

    今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...