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

cpr*_*ack 1 android vcf-vcard import-contacts contactscontract android-contacts

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

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

唯一的问题是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");                                                                
Run Code Online (Sandbox Code Playgroud)

关于如何处理这个问题的任何想法?

cpr*_*ack 5

回答我自己的问题.我终于找到了一种通过联系人app自动打开vCard的方法,这要归功于关于显式意图的答案.

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)
    }
}
Run Code Online (Sandbox Code Playgroud)