Android: 使用意图共享纯文本(对所有消息应用程序)

我试图用一个意图来分享一些文本:

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,SMS...) 尝试使用 android.content.Intent.ACTION_VIEW 尝试使用 i.setType("vnd.android-dir/mms-sms");没有任何帮助..。

(仅使用短信共享 "vnd.android-dir/mms-sms"!)

160831 次浏览

使用代码作为:

    /*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 中与意图共享的很好的例子:

* 在 Android 中与意图共享

//Share text:


Intent intent2 = new Intent(); intent2.setAction(Intent.ACTION_SEND);
intent2.setType("text/plain");
intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );
startActivity(Intent.createChooser(intent2, "Share via"));


//via Email:


Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_SEND);
intent2.setType("message/rfc822");
intent2.putExtra(Intent.EXTRA_EMAIL, new String[]{EMAIL1, EMAIL2});
intent2.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );
startActivity(intent2);


//Share Files:


//Image:


boolean isPNG = (path.toLowerCase().endsWith(".png")) ? true : false;


Intent i = new Intent(Intent.ACTION_SEND);
//Set type of file
if(isPNG)
{
i.setType("image/png");//With png image file or set "image/*" type
}
else
{
i.setType("image/jpeg");
}


Uri imgUri = Uri.fromFile(new File(path));//Absolute Path of image
i.putExtra(Intent.EXTRA_STREAM, imgUri);//Uri of image
startActivity(Intent.createChooser(i, "Share via"));
break;


//APK:


File f = new File(path1);
if(f.exists())
{


Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_SEND);
intent2.setType("application/vnd.android.package-archive");//APk file type
intent2.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f) );
startActivity(Intent.createChooser(intent2, "Share via"));
}
break;

使用下面的方法,只需将主题和主体作为 方法

public static void shareText(String subject,String body) {
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("text/plain");
txtIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
txtIntent .putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(txtIntent ,"Share"));
}

此代码用于通过短信共享

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

新的方法是使用 ShareCompat:

// 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 开发者倡导者 Ian Lake 写的这篇很棒的文章对该 API 进行更完整的分解。正如您将注意到的,我从那篇文章中借用了一些这样的例子。

如果那篇文章没有回答你所有的问题,Android 开发者网站上总是有 用于 ShareCompat.InentBuilder 的 Javadoc 本身。我在这个基于 Clemantiano 的评论。的 API 使用示例中添加了更多内容

100% Gmail 共享工作代码

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

通过使用 ACTION_SEND创建一个意图,您将能够添加额外的类型为 Intent.EXTRA_TEXT,第二个参数是您想要共享的文本。 然后,通过将共享类型设置为 text/plain,意图服务将为您提供所有支持共享文本的应用程序

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

科特林

在点击监听器内部,需要添加这个模块,以便通过诸如 whatsApp、 Gmail、 Slack 等应用程序共享文本。

shareOptionClicked.setOnClickListener{
val shareText = Intent(Intent.ACTION_SEND)
shareText.type = "text/plain"
val dataToShare = "Message from my application"
shareText.putExtra(Intent.EXTRA_SUBJECT, "Subject from my application")
shareText.putExtra(Intent.EXTRA_TEXT, dataToShare)
startActivity(Intent.createChooser(shareText, "Share Via"))
}