0

我正在创建一个应用程序,其中有一个 Gmail 按钮,当用户单击 Gmail 时,它会使用内置的 gmail 客户端向特定的收件人发送邮件,但是当用户单击 gmail 按钮时,它会打开一个所有的列表安装在设备中的客户端,例如远足、驱动器、电子邮件、gmail 等,用户要求选择。

这是我的代码

       protected void sendEmail() {
        Log.i("Send email", "");
        String[] TO = {""};
        String[] CC = {""};
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("message/rfc822");   //should i have to change this line of code
        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.EXTRA_CC, CC);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");  //message subject
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");   //message content

        try {
            startActivity(Intent.createChooser(emailIntent,"Send mail..."));
            finish();
            Log.i("Finished sending email.", "");
        }
        catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MailSenderActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
        }
    }

我想要的是当用户点击 Gmail 按钮时,它只会打开内置的 Gmail 和电子邮件客户端,而不会加息等等。我怎样才能实现这一点(通过改变类型或什么?)。我尝试了很多方法,但都是徒劳的。

4

3 回答 3

1

您可以使用以下代码重定向到 Gmail 而无需其他电子邮件应用程序

final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setType("plain/text");

        PackageInfo pInfo = null;
        try {
            pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = "";
        if (pInfo != null) {
            version = pInfo.versionName;
        }

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

        intent.putExtra(Intent.EXTRA_EMAIL, "abc.gmail.com");
        intent.putExtra(Intent.EXTRA_SUBJECT,"subject here !");
        intent.putExtra(Intent.EXTRA_TEXT,"Body here !");
        startActivity(intent);
于 2017-01-09T11:54:45.437 回答
0

这仅适用于 gmail 应用程序。

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("plain/text");
sendIntent.setData(Uri.parse("test@gmail.com"));
sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test@gmail.com" });
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "test");
sendIntent.putExtra(Intent.EXTRA_TEXT, "hello. this is a message sent from my demo app :-)");
startActivity(sendIntent);

用于大量电子邮件:

sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test@gmail.com" });

对于单个电子邮件:

sendIntent.setData(Uri.parse("test@gmail.com"));

您可以添加额外的“putExtra(Intent.EXTRA..)”并根据您的目的更改“setType”。

于 2017-01-09T10:41:21.870 回答
0

这是我的代码

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[]{"exampleto@gmail.com"});
        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);

或者您可以使用以下代码

Intent intent = new Intent(Intent.ACTION_SEND);

String[] strTo = { getString(R.string.mailto) };

intent.putExtra(Intent.EXTRA_EMAIL, strTo);
intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.mail_subject));
intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.mail_body));

Uri attachments = Uri.parse(image_path);
intent.putExtra(Intent.EXTRA_STREAM, attachments);

intent.setType("message/rfc822");

intent.setPackage("com.google.android.gm");

startActivity(intent);
于 2017-01-09T10:52:44.863 回答