Android Intent Chooser

时间:2017-01-09 10:32:16

标签: android android-intent android-intent-chooser

我正在创建一个应用程序,其中有一个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和电子邮件客户端中打开,而不会加息。我怎样才能实现这一点(通过改变类型或什么?)。我尝试了很多方法,但徒劳无功。

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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”。

答案 2 :(得分: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);