2012-12-12 61 views
10

有沒有辦法以程序化方式打開電子郵件客戶端,而不需要強制發送消息?我只想應用,讓用戶打開他的電子郵件客戶端的電子郵件檢查的目的:)通過意向打開電子郵件客戶端(但不發送消息)

Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("message/rfc822"); 
    startActivity(Intent.createChooser(intent, "")); 

此代碼的工作,但它迫使用戶發送新郵件。

回答

1

我想你應該更換Intent.ACTION_SENDIntent.ACTION_VIEW
我相信這會工作,因爲這將與應用程序,它支持MIME類型"message/rfc822"所以它將列表提示請將您的默認電子郵件客戶端包含在gmail應用以外的設備中。

這個怎麼樣代碼:

final Intent emailLauncher = new Intent(Intent.ACTION_VIEW); 
emailLauncher.setType("message/rfc822"); 
try{ 
     startActivity(emailLauncher); 
}catch(ActivityNotFoundException e){ 

} 
19
Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_APP_EMAIL); 
    startActivity(intent); 
    startActivity(Intent.createChooser(intent, getString(R.string.ChoseEmailClient))); 

有點工作。但是運行結束的Gmail對我來說,甚至因爲我有其他電子郵件客戶端

+1

應該使用try catch來啓動activity以避免例如沒有電子郵件應用程序的異常。 – ademar111190

+0

你有沒有設法讓選擇器工作?它爲我打開gmail,並且我安裝了另一個電子郵件客戶端(myMail)。 – user1354603

+0

您可以省略最後一行。調用'startActivity(intent);'隱式地打開選擇器(除非設置了默認值),所以'createChooser'不是必需的。 –

4

該代碼會顯示與電子郵件客戶端列表的對話框。點擊其中一個將啓動應用程序:

try { 
    List<String> emailClientNames = new ArrayList<String>(); 
    final List<String> emailClientPackageNames = new ArrayList<String>(); 
    // finding list of email clients that support send email 
    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
     "mailto", "[email protected]", null)); 
    PackageManager pkgManager = AppController.getContext().getPackageManager(); 
    List<ResolveInfo> packages = pkgManager.queryIntentActivities(intent, 0); 
    if (!packages.isEmpty()) { 
     for (ResolveInfo resolveInfo : packages) { 
      // finding the package name 
      String packageName = resolveInfo.activityInfo.packageName; 
      emailClientNames.add(resolveInfo.loadLabel(getPackageManager()).toString()); 
      emailClientPackageNames.add(packageName); 
     } 
     // a selection dialog for the email clients 
     AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this); 
     builder.setTitle("Select email client"); 
     builder.setItems(emailClientNames.toArray(new String[]{}), new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // on click we launch the right package 
      Intent intent = getPackageManager().getLaunchIntentForPackage(emailClientPackageNames.get(which)); 
       startActivity(intent); 
      } 
     }); 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 
    } 
} catch (ActivityNotFoundException e) { 
    // Show error message 
} 
+0

這適用於我!非常感謝你。 –

相關問題