在Android中将文本从应用程序共享到电子邮件应用

时间:2016-05-03 15:06:35

标签: android email android-intent share

我有以下代码用于将文本从我的应用程序发送到电子邮件:

Intent mail = new Intent(Intent.ACTION_VIEW);
            mail.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
                mail.putExtra(Intent.EXTRA_EMAIL, new String[] {  });
                mail.setData(Uri.parse(""));
                mail.putExtra(Intent.EXTRA_SUBJECT, "Country Decryption");
                mail.setType("plain/text");
                mail.putExtra(Intent.EXTRA_TEXT, "my text");
                ctx.startActivity(mail);  

它有效,但如您所见,它使用Gmail应用,如何使用电子邮件应用而不是Gmail?

我的意思是这个应用: email app

如何分享到Facebook?我发现Facebook不再支持使用意图共享,我必须使用Facebook SDK,但我找不到任何简单的程序来做到这一点。有什么简单的方法吗?

问候。

4 个答案:

答案 0 :(得分:4)

很好地使用其他电子邮件应用程序,我担心你必须创建一个选择器对话框,让用户选择使用哪个应用程序,如下所示

var theActiveData = function() {
    return $('ul li.hovering').attr("data-tag");
}
alert(theActiveData);

答案 1 :(得分:1)

  1. 您已将Gmail应用程序设置为默认的EMAIL应用程序。 使用Intent.createChooser列出支持您意图的所有应用程序。
  2. 示例:

    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    /* Fill it with Data */
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"to@email.com"});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");
    /* Send it off to the Activity-Chooser */
    context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    
    1. 对于Facebook,您可以点击以下链接。

      Android and Facebook share intent

答案 2 :(得分:1)

您必须在ACTION_SEND上创建特定过滤器,然后才能阅读完整的答案here

您可以在此代码中选择应用选择器中显示的应用

List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();        
    for (int i = 0; i < resInfo.size(); i++) {
        // Extract the label, append it, and repackage it in a LabeledIntent
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        if(packageName.contains("android.email")) {
            emailIntent.setPackage(packageName);
        } else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");
            if(packageName.contains("twitter")) {
                intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
            } else if(packageName.contains("facebook")) {
                // Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
                // One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
                // will show the <meta content ="..."> text from that page with our link in Facebook.
                intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
            } else if(packageName.contains("mms")) {
                intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
            } else if(packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
                intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
                intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));               
                intent.setType("message/rfc822");
            }

            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }
    }

答案 3 :(得分:0)

这只适用于gmail应用程序。

cluster