使用联系人应用程序直接打开.vcf vCard文件

时间:2014-03-05 17:53:44

标签: android android-contacts contactscontract vcard import-contacts

我正在尝试以编程方式启动联系人应用以导入包含大量联系人的大型.vcf文件。以下代码几乎完美无缺。

String vcfMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("vcf");
Intent openVcfIntent = new Intent(Intent.ACTION_VIEW);
openVcfIntent.setDataAndType(Uri.fromFile(savedVCardFile), vcfMimeType);
startActivity(openVcfIntent);

唯一的问题是Android会显示应用选择器对话框,不仅会显示联系人应用,还会显示Dropbox(或任何其他处理vCard文件的应用)。我想阻止此行为,并使用联系人应用程序直接打开文件,以便导入自动启动。

我在StackOverflow上尝试了几件没有运气的东西,比如设置:

openVcfIntent.setComponent(new ComponentName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity"));
openVcfIntent.setAction("android.intent.action.MAIN");                                                                       
openVcfIntent.addCategory("android.intent.category.LAUNCHER");                                                               
openVcfIntent.addCategory("android.intent.category.DEFAULT");                                                                

关于如何解决问题的任何想法?

1 个答案:

答案 0 :(得分:5)

回答我自己的问题。我终于找到了一种通过联系人应用程序自动打开vCard的方法,感谢this answer about explicit intents

private void openBackup(File savedVCard)
{
    try
    {
        String vcfMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("vcf");
        Intent openVcfIntent = new Intent(Intent.ACTION_VIEW);
        openVcfIntent.setDataAndType(Uri.fromFile(savedVCard), vcfMimeType);
        // Try to explicitly specify activity in charge of opening the vCard so that the user doesn't have to choose
        // https://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android/9229654#9229654
        try
        {
            if (getActivity().getPackageManager() != null)
            {
                List<ResolveInfo> resolveInfos = getActivity().getPackageManager().queryIntentActivities(openVcfIntent, 0);
                if (resolveInfos != null)
                {
                    for (ResolveInfo resolveInfo : resolveInfos)
                    {
                        ActivityInfo activityInfo = resolveInfo.activityInfo;
                        if (activityInfo != null)
                        {
                            String packageName = activityInfo.packageName;
                            String name = activityInfo.name;
                            // Find the needed Activity based on Android source files: http://grepcode.com/search?query=ImportVCardActivity&start=0&entity=type&n=
                            if (packageName != null && packageName.equals("com.android.contacts") && name != null && name.contains("ImportVCardActivity"))
                            {
                                openVcfIntent.setPackage(packageName);
                                break;
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ignored)
        {
        }

        startActivity(openVcfIntent);
    }
    catch (Exception exception)
    {
        // No app for openning .vcf files installed (unlikely)
    }
}
相关问题