可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to share some text using an intent:
Intent i = new Intent(android.content.Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(android.content.Intent.EXTRA_TEXT, "TEXT");
and warping with chooser:
startActivity(Intent.createChooser(sms, getResources().getString(R.string.share_using)));
it works! but only for email app.
what I need is a general intent for all messaging app: emails, sms, IM (Whatsapp, Viber, Gmail, SMS...) tried using android.content.Intent.ACTION_VIEW
and tried using i.setType("vnd.android-dir/mms-sms");
nothing helped...
("vnd.android-dir/mms-sms"
shared using sms only!)
回答1:
Use the code as:
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)));
回答2:
New way of doing this would be using ShareCompat.IntentBuilder like so:
// 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();
If you have any more questions about using ShareCompat, I highly recommend this great article from Ian Lake, an Android Developer Advocate at Google, for a more complete breakdown of the API. As you'll notice, I borrowed some of this example from that article.
If that article doesn't answer all of your questions, there is always the Javadoc itself for ShareCompat.IntentBuilder on the Android Developers website. I added more to this example of the API's usage on the basis of clemantiano's comment.
回答3:
This a great example about share with Intents in Android:
* Share with Intents in 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;
回答4:
Use below method, just pass the subject and body as arguments of the method
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")); }
回答5:
Below is the code that works with both the email or messaging app. If you share through email then the subject and body both are added.
Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setType("text/plain"); String shareString = Html.fromHtml("Medicine Name:" + medicine_name + "Store Name:" + “store_name “+ "
" + "Store Address:" + “store_address” + "
") .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(); }
回答6:
This code is for sharing through 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);
回答7:
Images or binary data:
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"));
or HTML:
Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setType("text/html"); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("This is the text shared.
")); startActivity(Intent.createChooser(sharingIntent,"Share using"));