2012-09-06 4 views
2

사용자가 createChooser() 대화 상자에서 첫 번째 옵션을 선택한 것처럼 지정된 인 텐트를 처리하는 첫 번째 응용 프로그램을 자동으로 가져 오거나 선택합니다. 이 예에서 createchooser에서 자동으로 이메일 앱을 선택하는 방법은 무엇입니까?

은 이메일과 같은 데이터를 전송 응용 프로그램 사이에서 선택 :

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); SendEmailActivity.this.startActivity(Intent.createChooser(1, "Send mail..."));

이 도와주세요. 사용

답변

4

여기 android.content.Intent.ACTION_SEND

내가 아래 코딩 작업 예제의 의도와 모든 응용 프로그램의 목록을 얻을 수 있습니다 answerquestion

** 공정한 경고 (내 장치에서 Gmail을 따기 끝) - 어떤 이메일 계정 설정 없었다 경우 변수 pkgAppsList에 널 체크를 추가해야 NullPointerException이 던져 사용자에게 발견에는 전자 메일 응용 프로그램을 말할 또는 설치

//set the main intent to ACTION_SEND for looking for applications that share information 
    Intent intent = new Intent(Intent.ACTION_SEND, null); 

    //intent.addCategory(Intent.CATEGORY_LAUNCHER); //if you want extra filters 

    //filter out apps that are able to send plain text 
    intent.setType("plain/text"); 

    //get a list of apps that meet your criteria above 
    List<ResolveInfo> pkgAppsList = this.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_RESOLVED_FILTER); 

    //select the first one in the list 
    ResolveInfo info = pkgAppsList.get(0);  
    String packageName = info.activityInfo.packageName; 
    String className = info.activityInfo.name; 

    //set the intent to luanch that specific app 
    intent.setClassName(packageName, className); 

    //some samples on adding more then one email address 
    String aEmailList[] = { "[email protected]","[email protected]" }; 
    String aEmailCCList[] = { "[email protected]","[email protected]"}; 
    String aEmailBCCList[] = { "[email protected]" }; 

    //all the extras that will be passed to the email app  
    intent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 
    intent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList); 
    intent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList); 
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject"); 
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "My message body."); 

    //start the app 
    startActivity(intent); 
,369에게 있었다 것

어떤 앱을 화이트리스트에 추가하려면 특정 패키지의 각 패키지 이름을 확인하는 목록을 반복 할 수 있습니다 (예 : gmail은 "com.google.android.gm"입니다).

+0

답장을 보내 주셔서 감사합니다.하지만 자동 채집 응용 프로그램이 아직 단계별로 도움이되지 않습니다. 고맙습니다. – Japz

+0

코드를 작성하고 테스트하려면 – StrikeForceZero

+0

@Japz가 작업 코드 – StrikeForceZero

2

StrikeForceZero가 게시됨에 따라 패키지 이름을 기반으로 목록에서 Gmail 앱을 찾도록 자동 선택 우선 선택 항목을 변경했습니다. Gmail 앱이 항상 목록에 처음 나오는 것은 아니기 때문에 나를 위해 일했습니다. 신용은 StrikeFirstZero로 이동합니다 ... 나는 방금 자신의 코드에 작은 수정을하였습니다.

//set the main intent to ACTION_SEND for looking for applications that share information 
    Intent intent = new Intent(Intent.ACTION_SEND, null); 

    //intent.addCategory(Intent.CATEGORY_LAUNCHER); //if you want extra filters 

    //filter out apps that are able to send plain text 
    intent.setType("plain/text"); 

    //get a list of apps that meet your criteria above 
    List<ResolveInfo> pkgAppsList = activity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_RESOLVED_FILTER); 

    //Cycle through list of apps in list and select the one that matches GMail's package name 
    for (ResolveInfo resolveInfo : pkgAppsList) { 
     String packageName = resolveInfo.activityInfo.packageName; 
     String className = ""; 
     if(packageName.equals("com.google.android.gm")) { 
      className = resolveInfo.activityInfo.name; 
      intent.setClassName(packageName, className); 
     } 
    } 

    //some samples on adding more then one email address 
    String aEmailList[] = { "[email protected]","[email protected]" }; 
    String aEmailCCList[] = { "[email protected]","[email protected]"}; 
    String aEmailBCCList[] = { "[email protected]" }; 

    //all the extras that will be passed to the email app 
    intent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList); 
    intent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList); 
    intent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList); 
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject"); 
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "My message body."); 

    //start the app 
    activity.startActivity(intent); 
관련 문제