Android:使用intent共享纯文本(适用于所有消息传递应用程序)

Android:使用intent共享纯文本(适用于所有消息传递应用程序),android,email,android-intent,sms,Android,Email,Android Intent,Sms,我正在尝试使用以下意图共享一些文本: Intent i = new Intent(android.content.Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(android.content.Intent.EXTRA_TEXT, "TEXT"); 和使用选择器进行扭曲: startActivity(Intent.createChooser(sms, getResources().getString(R.string.sha

我正在尝试使用以下意图共享一些文本:

Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");  
i.putExtra(android.content.Intent.EXTRA_TEXT, "TEXT");
和使用选择器进行扭曲:

startActivity(Intent.createChooser(sms, getResources().getString(R.string.share_using)));
它起作用了!但仅适用于电子邮件应用程序。
我需要的是所有短信应用程序的一般意图:电子邮件、短信、即时消息(Whatsapp、Viber、Gmail、短信…) 尝试使用android.content.Intent.ACTION\u视图

并尝试使用
i.setType(“vnd.android dir/mms-sms”)没有任何帮助

“vnd.android dir/mms sms”
仅使用sms共享!)

将代码用作:

    /*Create an ACTION_SEND Intent*/
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    /*This will be the actual content you wish you share.*/
    String shareBody = "Here is the share content body";
    /*The type of the content is text, obviously.*/
    intent.setType("text/plain");
    /*Applying information Subject and Body.*/
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_subject));
    intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    /*Fire!*/
    startActivity(Intent.createChooser(intent, getString(R.string.share_using)));

这是一个关于Android中意向共享的好例子:

使用下面的方法,只需将subject和body作为 方法


此代码用于通过sms共享

     String smsBody="Sms Body";
     Intent sendIntent = new Intent(Intent.ACTION_VIEW);
     sendIntent.putExtra("sms_body", smsBody);
     sendIntent.setType("vnd.android-dir/mms-sms");
     startActivity(sendIntent);
图像或二进制数据:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
或HTML:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text shared.</p>"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));
Intent sharingcontent=新的意图(Intent.ACTION\u SEND);
sharingcontent.setType(“text/html”);
sharingint.putExtra(android.content.Intent.EXTRA_TEXT,Html.fromHtml(“这是共享的文本。

”); startActivity(Intent.createChooser(共享内容,“共享使用”);
以下是电子邮件或消息应用程序的代码。 如果您通过电子邮件共享,则主题和正文都会添加

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

                String shareString = Html.fromHtml("Medicine Name:" + medicine_name +
                        "<p>Store Name:" + “store_name “+ "</p>" +
                        "<p>Store Address:" + “store_address” + "</p>")  .toString();
                                      sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Medicine Enquiry");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareString);

                if (sharingIntent.resolveActivity(context.getPackageManager()) != null)
                    context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
                else {
                    Toast.makeText(context, "No app found on your phone which can perform this action", Toast.LENGTH_SHORT).show();
                }
Intent sharingcontent=新的意图(Intent.ACTION\u SEND);
sharingcontent.setType(“文本/普通”);
String shareString=Html.fromHtml(“药品名称:”+Medicine\u名称+
商店名称:“+”商店名称“+”

”+ 存储地址:“+”存储地址“+”

”。toString(); 分享内容。putExtra(android.content.Intent.EXTRA_主题,“药品查询”); sharingcontent.putExtra(android.content.Intent.EXTRA_TEXT,shareString); if(sharingcontent.resolveActivity(context.getPackageManager())!=null) context.startActivity(Intent.createChooser(共享内容,“共享使用”); 否则{ Toast.makeText(上下文,“手机上找不到可以执行此操作的应用程序”,Toast.LENGTH_SHORT.show(); }
新的方法是使用ShareCompat.IntentBuilder,如下所示:

// Create and fire off our Intent in one fell swoop
ShareCompat.IntentBuilder
        // getActivity() or activity field if within Fragment
        .from(this) 
        // The text that will be shared
        .setText(textToShare)
        // most general text sharing MIME type
        .setType("text/plain") 
        .setStream(uriToContentThatMatchesTheArgumentOfSetType)
        /*
         * [OPTIONAL] Designate a URI to share. Your type that 
         * is set above will have to match the type of data
         * that your designating with this URI. Not sure
         * exactly what happens if you don't do that, but 
         * let's not find out.
         * 
         * For example, to share an image, you'd do the following:
         *     File imageFile = ...;
         *     Uri uriToImage = ...; // Convert the File to URI
         *     Intent shareImage = ShareCompat.IntentBuilder.from(activity)
         *       .setType("image/png")
         *       .setStream(uriToImage)
         *       .getIntent();
         */
        .setEmailTo(arrayOfStringEmailAddresses)
        .setEmailTo(singleStringEmailAddress)
        /*
         * [OPTIONAL] Designate the email recipients as an array
         * of Strings or a single String
         */ 
        .setEmailTo(arrayOfStringEmailAddresses)
        .setEmailTo(singleStringEmailAddress)
        /*
         * [OPTIONAL] Designate the email addresses that will be 
         * BCC'd on an email as an array of Strings or a single String
         */ 
        .addEmailBcc(arrayOfStringEmailAddresses)
        .addEmailBcc(singleStringEmailAddress)
        /* 
         * The title of the chooser that the system will show
         * to allow the user to select an app
         */
        .setChooserTitle(yourChooserTitle)
        .startChooser();
如果您对使用ShareCompat还有任何疑问,我强烈建议谷歌的Android开发者拥护者对API进行更完整的分解

如果那篇文章没有回答你所有的问题,Android开发者网站上总是会有一些问题。我在这个例子中添加了更多API的用法,基于Gmail共享的100%工作代码


但我不明白是什么造成了区别??只是外部主体字符串??没有区别。在emulator上,我打开了消息应用程序,但在我的手机和平板电脑上,我被要求从应用程序列表中进行选择。可能是关于在emulator上安装这些额外的应用程序。回答很好!有人能告诉我,如果忽略sharingcontent.setType(“text/plain”);
part?如何仅为whatsup设置单独的文本在intent sharingcontent.setPackage(“com.whatsapp”)中添加以下代码段;除了这个答案之外,还有设置电子邮件地址收件人的方法,如setEmailBcc()、setEmailCc()和setEmailTo()。感谢分享,但它对我来说并不完美,有时我会遇到以下异常java.lang.IllegalArgumentException:服务未注册:ActivityInfo{67f62c5 com.google.android.apps.hangouts.phone.ShareIntentActivity}
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");

                String shareString = Html.fromHtml("Medicine Name:" + medicine_name +
                        "<p>Store Name:" + “store_name “+ "</p>" +
                        "<p>Store Address:" + “store_address” + "</p>")  .toString();
                                      sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Medicine Enquiry");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareString);

                if (sharingIntent.resolveActivity(context.getPackageManager()) != null)
                    context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
                else {
                    Toast.makeText(context, "No app found on your phone which can perform this action", Toast.LENGTH_SHORT).show();
                }
// Create and fire off our Intent in one fell swoop
ShareCompat.IntentBuilder
        // getActivity() or activity field if within Fragment
        .from(this) 
        // The text that will be shared
        .setText(textToShare)
        // most general text sharing MIME type
        .setType("text/plain") 
        .setStream(uriToContentThatMatchesTheArgumentOfSetType)
        /*
         * [OPTIONAL] Designate a URI to share. Your type that 
         * is set above will have to match the type of data
         * that your designating with this URI. Not sure
         * exactly what happens if you don't do that, but 
         * let's not find out.
         * 
         * For example, to share an image, you'd do the following:
         *     File imageFile = ...;
         *     Uri uriToImage = ...; // Convert the File to URI
         *     Intent shareImage = ShareCompat.IntentBuilder.from(activity)
         *       .setType("image/png")
         *       .setStream(uriToImage)
         *       .getIntent();
         */
        .setEmailTo(arrayOfStringEmailAddresses)
        .setEmailTo(singleStringEmailAddress)
        /*
         * [OPTIONAL] Designate the email recipients as an array
         * of Strings or a single String
         */ 
        .setEmailTo(arrayOfStringEmailAddresses)
        .setEmailTo(singleStringEmailAddress)
        /*
         * [OPTIONAL] Designate the email addresses that will be 
         * BCC'd on an email as an array of Strings or a single String
         */ 
        .addEmailBcc(arrayOfStringEmailAddresses)
        .addEmailBcc(singleStringEmailAddress)
        /* 
         * The title of the chooser that the system will show
         * to allow the user to select an app
         */
        .setChooserTitle(yourChooserTitle)
        .startChooser();
    Intent intent = new Intent (Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"anyMail@gmail.com"});
    intent.putExtra(Intent.EXTRA_SUBJECT, "Any subject if you want");
    intent.setPackage("com.google.android.gm");
    if (intent.resolveActivity(getPackageManager())!=null)
        startActivity(intent);
    else
        Toast.makeText(this,"Gmail App is not installed",Toast.LENGTH_SHORT).show();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");

Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);