2010-10-14 123 views

回答

34

我不知道,你可以明确启动Gmail。你有没有在你的onClickListener尝试这种

Intent emailIntent = new Intent(Intent.ACTION_SEND); 
emailIntent.setType("text/plain"); 
startActivity(emailIntent); 

你可以找到更多的细节在这里:Email android intent

+0

不,我没有尝试这样做呢。但我肯定会。感谢杰夫的帮助。 – Aakash 2010-10-14 16:28:53

+0

文字/平原作品 – Aakash 2010-10-16 15:21:40

+0

感谢链接也 – 2011-05-13 11:02:16

41

由于JeffC指出,很容易基本上是告诉你要送东西电子邮件等,并有Android的安卓给用户一个选择列表,其中可能包括GMail。如果你特别想要GMail,你必须有点聪明。 (请注意,正确的MIME类型实际上是“text/plain的”,而不是“纯/文本”。做一个实现古怪,GMail的似乎是这是为了响应后者唯一的活动,但是这不是我的行为会指望)

下面的程序演示了可以遵循的原则:其实检查所有这一切说,他们可以处理你的意图SEND,看看其中是否像Gmail中的活动。

package com.stackoverflow.beekeeper; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.os.Bundle; 

import java.util.List; 

public class StackOverflowTest extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
     intent.setType("text/plain"); 
     final PackageManager pm = getPackageManager(); 
     final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0); 
     ResolveInfo best = null; 
     for (final ResolveInfo info : matches) 
      if (info.activityInfo.packageName.endsWith(".gm") || 
     info.activityInfo.name.toLowerCase().contains("gmail")) best = info; 
     if (best != null) 
      intent.setClassName(best.activityInfo.packageName, best.activityInfo.name); 
     startActivity(intent); 
    } 
} 
+0

感谢您的信息和代码beekeeper。对于这个问题打开任何其他邮件程序,让用户选择是完全正确的。它不一定是Gmail。只有那个程序应该能够发送一个我认为是通过最终Intent intent = new Intent(android.content.Intent.ACTION_SEND)完成的电子邮件; – Aakash 2010-10-14 18:38:39

+0

如何以编程方式打开Yahoo邮件? – 2013-03-26 11:33:37

+0

也许这个答案会帮助你:http://stackoverflow.com/questions/9516334/how-to-open-gmail-yahoo-mail-and-rediff-mails-in-application-programmatically – anivaler 2013-04-29 09:32:53

31
try{  
    Intent intent = new Intent (Intent.ACTION_VIEW , Uri.parse("mailto:" + "your_email")); 
    intent.putExtra(Intent.EXTRA_SUBJECT, "your_subject"); 
    intent.putExtra(Intent.EXTRA_TEXT, "your_text"); 
    startActivity(intent); 
}catch(ActivityNotFoundException e){ 
    //TODO smth 
} 
+0

这是唯一对我有用的答案。 – Jacolack 2017-01-16 08:45:13

+1

有用,用ActivityNotFoundException包装它。 – Rahul 2017-03-11 08:56:38

+1

@Rasi谢谢。我在答案中添加了try-catch。 – anivaler 2017-03-13 08:32:58

0

如果你没有在该行得到任何

final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);

然后替换

final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 1);

1

这这行代码就会直接启动的Gmail应用程序发送电子邮件。

我发现使用this post,这里的重要组成部分,是要找到“软件包名”,我想只能用gmail没有选择器的“activityInfo.name”

。请注意,软件包名称是硬编码的,所以如果Gmail更改其包名称,它将不再起作用。

的关键,这是其中第一参数是包名和所述第二参数是所述activityInfo名称setComponent。

但就像我说的谨慎使用,我重复自己;如果用户没有安装gmail应用程序,或者gmail更改其包名或Activty名称以发送电子邮件,则该(硬)代码将会中断。你已经被警告;)

这里是我的代码

Intent myIntent = new Intent(Intent.ACTION_SEND); 

PackageManager pm = getPackageManager(); 
Intent tempIntent = new Intent(Intent.ACTION_SEND); 
tempIntent.setType("*/*"); 
List<ResolveInfo> resInfo = pm.queryIntentActivities(tempIntent, 0); 
for (int i = 0; i < resInfo.size(); i++) { 
    ResolveInfo ri = resInfo.get(i); 
    if (ri.activityInfo.packageName.contains("android.gm")) { 
     myIntent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name)); 
     myIntent.setAction(Intent.ACTION_SEND); 
     myIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     myIntent.setType("message/rfc822"); 
     myIntent.putExtra(Intent.EXTRA_TEXT, "extra text"); 
     myIntent.putExtra(Intent.EXTRA_SUBJECT, "Extra subject"); 
     myIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("uri://your/uri/string"); 
    } 
} 
startActivity(myIntent); 
2
public static void openGmail(Activity activity,String[] email, String subject, String content) { 
    Intent emailIntent = new Intent(Intent.ACTION_SEND); 
    emailIntent.putExtra(Intent.EXTRA_EMAIL, email); 
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    emailIntent.setType("text/plain"); 
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content); 
    final PackageManager pm = activity.getPackageManager(); 
    final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0); 
    ResolveInfo best = null; 
    for(final ResolveInfo info : matches) 
     if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) 
      best = info; 
    if (best != null) 
     emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name); 

    activity.startActivity(emailIntent); 
} 
+0

它打开Gmail应用程序,但一旦电子邮件发送它不会去收件人 – 2016-12-29 11:45:00

2
<TextView 
android:id="@+id/EmailId" 
android:linksClickable="true" 
android:autoLink="email" 
android:text="[email protected]" 
/> 

这是发送电子邮件上的TextView的点击是最好的方法。

0
Intent intent = new Intent(Intent.ACTION_SEND).setType("text/plain") 
         .putExtra(Intent.EXTRA_EMAIL, new String[]{emails}); 
       List<ResolveInfo> matches = activity.getPackageManager().queryIntentActivities(intent, 0); 
       ResolveInfo best = null; 
       for (ResolveInfo info : matches) { 
        if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) { 
         best = info; 
        } 
       } 
       if (best != null) { 
        intent.setClassName(best.activityInfo.packageName,best.activityInfo.name); 
       } 
       activity.startActivity(intent); 
+0

请添加一些额外的信息,为什么这将工作 – plaidDK 2017-11-07 11:13:20

+0

@ plaidDK,在列表,我们正在获取设备上安装的应用程序的所有包。之后,通过将条件放入for循环来检查gmail的包。如果列表包含gmail包,那么通过intent.setClassName()设置gmail包,它会直接打开gmail应用,否则会打开多个选项intent .. – 2017-11-08 06:50:48

1

您只需将下面的代码点击事件中。将直接打开gmail作为撰写模式,输出截图如下。

快乐编码:-)

代码:

Intent intent=new Intent(Intent.ACTION_SEND); 
String[] recipients={"[email protected]"}; 
intent.putExtra(Intent.EXTRA_EMAIL, recipients); 
intent.putExtra(Intent.EXTRA_SUBJECT,"Subject text here..."); 
intent.putExtra(Intent.EXTRA_TEXT,"Body of the content here..."); 
intent.putExtra(Intent.EXTRA_CC,"[email protected]"); 
intent.setType("text/html"); 
intent.setPackage("com.google.android.gm"); 
startActivity(Intent.createChooser(intent, "Send mail")); 

输出:

enter image description here

相关问题