【问题标题】:Open .vcf vCard file directly with contacts app使用联系人应用程序直接打开 .vcf vCard 文件
【发布时间】:2014-04-07 23:03:56
【问题描述】:

我正在尝试以编程方式启动联系人应用程序以导入包含大量联系人的大型 .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");                                                                

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

【问题讨论】:

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


    【解决方案1】:

    回答我自己的问题。感谢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)
        }
    }
    

    【讨论】:

    • 你在 getActivity() 中写了什么?
    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多