1

많은 연락처가있는 큰 .vcf 파일을 가져 오기 위해 연락처 프로그램을 프로그래밍 방식으로 시작하려고합니다. 다음 코드는 거의 완벽하게 작동합니다. 안드로이드는 드롭 박스 (또는 vCard를 파일을 처리하는 다른 응용 프로그램을) 연락처 응용 프로그램뿐만 아니라 보여 앱 선택기 대화 상자를 표시하지만 것을.vcf 연락처 카드로 직접 vCard 파일 열기

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

유일한 문제이다. 이 동작을 방지하고 주소록 응용 프로그램에서 직접 파일을 열어 가져 오기가 자동으로 시작되도록하고 싶습니다. 이 문제를 접근하는 방법에

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");                 

어떤 아이디어 :

나는 같은 설정으로 행운과에 StackOverflow에서 발견 몇 가지를 시도했습니다?

답변

4

내 질문에 답해주세요. this answer about explicit intents 덕택에 연락처 앱으로 자동으로 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) 
    } 
}