2014-02-12 159 views
15

我需要爲用戶提供功能,用戶可以通過發送電子郵件來共享一些數據。我使用下面的代碼。點擊按鈕發送Android應用程序的電子郵件

Intent email = new Intent(android.content.Intent.ACTION_SENDTO); 
    email.setType("message/rfc822"); 
    email.putExtra(Intent.EXTRA_EMAIL, new String[] { to }); 
    email.putExtra(Intent.EXTRA_SUBJECT, subject); 
    email.putExtra(Intent.EXTRA_TEXT, message); 
    startActivity(Intent.createChooser(email,"Choose an Email client :")); 

這顯示電子郵件,Gmail,Skype和通過藍牙發送供用戶選擇。我不希望用戶顯示Skype,通過藍牙發送到此列表中。我需要做什麼 ?我的手機裏有WhatsApp,它的功能相同,但不會在列表中顯示電子郵件,藍牙(設置 - >幫助 - >聯繫人 - > ...),只顯示列表中的電子郵件和Gmail。我需要這樣做。

回答

19

試試這個:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
      "mailto","[email protected]", null)); 
intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
intent.putExtra(Intent.EXTRA_TEXT, message); 
startActivity(Intent.createChooser(intent, "Choose an Email client :")); 

如果沒有特定的收件人 - 是這樣的:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
      "mailto", "", null)); 
+0

我檢查一下。我想只列表中的郵件客戶端(Gmail,雅虎,展望)不是Skype,通過藍牙發送。我該怎麼做..? – Amardeepvijay

+0

@Amardeep編輯我的答案。但是,WhatsApp仍然使用自定義對話框,即使您有兩個以上的電子郵件客戶端,該對話框也只會在列表中顯示兩個電子郵件客戶端(Gmail和電子郵件) – localhost

+0

它在實際設備中不起作用。我可以使用這個email.setType(「message/rfc822」); – Amardeepvijay

0

使用這種方法通過Gmail分享只有絕對需要調用

startActivity(getSendEmailIntent(context, email,subject, body)); 





public Intent getSendEmailIntent(Context context, String email, 
        String subject, String body) { 
      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

      try { 

       // Explicitly only use Gmail to send 
       emailIntent.setClassName("com.google.android.gm", 
         "com.google.android.gm.ComposeActivityGmail"); 

       emailIntent.setType("text/html"); 

       // Add the recipients 
       if (email != null) 
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
          new String[] { email }); 

       if (subject != null) 
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
          subject); 

       if (body != null) 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body)); 

       // Add the attachment by specifying a reference to our custom 
       // ContentProvider 
       // and the specific file of interest 
       // emailIntent.putExtra(
       // Intent.EXTRA_STREAM, 
       // Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/" 
       // + fileName)); 

       return emailIntent; 
    //   myContext.startActivity(emailIntent); 
      } catch (Exception e) { 
       emailIntent.setType("text/html"); 

       // Add the recipients 
       if (email != null) 
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
          new String[] { email }); 

       if (subject != null) 
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
          subject); 

       if (body != null) 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body)); 

    //   myContext.startActivity(Intent.createChooser(emailIntent, 
    //     "Share Via")); 
       return emailIntent; 
      } 
      } 
相關問題