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

我试图用意图分享一些文字:

Intent i = new Intent(android.content.Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(android.content.Intent.EXTRA_TEXT, "TEXT"); 

和select器翘曲:

 startActivity(Intent.createChooser(sms, getResources().getString(R.string.share_using))); 

有用! 但只适用于电子邮件应用
我需要的是所有消息应用程序的一般意图:电子邮件,短信,IM(WhatsApp,Viber,Gmail,SMS …)尝试使用android.content.Intent.ACTION_VIEW并尝试使用i.setType("vnd.android-dir/mms-sms"); 没有什么帮助

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

使用代码如下:

 String shareBody = "Here is the share content body"; Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here"); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using))); 

这样做的新方法将使用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有任何疑问,我强烈推荐Google的Android开发者倡导者Ian Lake撰写这篇优秀文章 ,以获得更完整的API分类。 你会注意到,我借用了这篇文章中的一些例子。

如果那篇文章没有回答你的所有问题,那么Android开发者网站上总是有对于ShareCompat.IntentBuilder的Javadoc本身 。 我在clemantiano的评论的基础上增加了这个API使用的例子。

这是一个很好的例子,在Android中与Intents分享:

*在Android中与Intents分享

 //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")); } 

以下是适用于电子邮件或消息应用程序的代码。 如果你通过电子邮件分享,那么主题和身体都被添加。

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

此代码是通过短信共享

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