调用系统程序导入手机上的vcard文件

这几天研究手机中vCard联系人的导入,直接调用系统程序静默导入,不弹出选择框。

刚开始没有头绪,从网上找的资料如下:

Uri uri = Uri.parse("file:///mnt/sdcard/vcard.vcf");

Intent intent = new Intent(Intent.ACTION_VIEW,uri);

intent.setType("text/x-vcard");

startActivity(intent);

这样的方法确实可以,可是会经常弹出选择框,让你自己去选择一次文件,比较烦,不是想要的结果。经过不懈的努力,终于得到了想要的结果。

代码如下:

Uri uri = Uri.parse("file:///mnt/sdcard/mVcard.vcf");

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(uri, "text/x-vcard");

//android系统版本不同,所以系统中的activity也会不同。

intent.setComponent(

new ComponentName("com.android.contacts","com.android.contacts.vcard.ImportVCardActivity"));

try{

startActivity(intent);

}catch(ActivityNotFoundException e){

intent.setComponent(

new ComponentName("com.android.contacts","com.android.contacts.ImportVCardActivity"));

startActivity(intent);

}

好了,结束,总算是完成了一件事,可以轻松一下了。

android 4.4 kitkat导入联系人又发生了变化,重新添加以下代码

intent.setComponent(

new ComponentName("com.android.contacts","com.android.contacts.common.vcard.ImportVCardActivity"));

        startActivity(intent);

4.4系统导入vcard成功。


完善代码,确保不同的系统都能够适配,所以在最后加上系统默认的,以免出错。

        Uri uri = Uri.parse("file:///mnt/sdcard/mVcard.vcf");

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(uri, "text/x-vcard");

//android系统版本不同,所以系统中的activity也会不同。

try{

intent.setComponent(

new ComponentName("com.android.contacts","com.android.contacts.vcard.ImportVCardActivity"));

startActivity(intent);

}catch(Exception e){

try{

intent.setComponent(

new ComponentName("com.android.contacts","com.android.contacts.ImportVCardActivity"));

startActivity(intent);

}catch(Exception e){

try{

intent.setComponent(

new ComponentName("com.android.contacts","com.android.contacts.common.vcard.ImportVCardActivity"));       

startActivity(intent);

}catch(Exception e){

intent.setComponent(null);

startActivity(intent);

}

}

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值