Android uses intent.action_send for simple sharing functions

Source: Internet
Author: User


Android, using Intent.action_send can achieve a simple "share" function, you can share text, pictures wait for other applications, such as QQ, SMS, etc.

Mainactivity.java file:

Package Com.example.androidtest;import Java.io.file;import Android.net.uri;import android.os.bundle;import Android.os.environment;import Android.app.activity;import Android.content.intent;import Android.view.Menu;public Class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); String strdlgtitle = "dialog box title-Share text"; String strsubject = "my subject"; String strcontent = "my share";/** * 1. Share Plain text content *///sharetext (Strdlgtitle, Strsubject, strcontent);/** * 2. Share images and text content */strdl gtitle = "dialog box title-Share Picture";//Picture file path (SD card root "1.png" picture) String Imgpath = Environment.getexternalstoragedirectory (). GetPath ( + File.separator + "1.png";//Picture Uriuri Imageuri = Uri.fromfile (new File (Imgpath));//Share shareimg (Strdlgtitle, Strsubject , Strcontent, Imageuri);} /** * Share text content * * @param dlgtitle * Share Dialog Title * @param subject * Theme * @param content * Share content (text) */private void Sharetext (String DLGtitle, string subject, string content) {if (content = = NULL | | ". Equals (content)) {return;} Intent Intent = new Intent (intent.action_send); Intent.settype ("Text/plain"); if (subject! = NULL &&! "). Equals (subject)) {Intent.putextra (intent.extra_subject, subject);} Intent.putextra (intent.extra_text, content);//Set PopOver caption if (dlgtitle! = null &&! "). Equals (Dlgtitle)) {//Custom title startactivity (Intent.createchooser (Intent, Dlgtitle));} else {//system default caption StartActivity ( Intent);}} /** * Share images and text content * * @param dlgtitle * Share Dialog Title * @param subject * Theme * @param content *  Enjoy content (text) * @param uri * Picture resource uri */private void shareimg (String dlgtitle, String subject, String Content,uri URI) {if (URI = = null) {return;} Intent Intent = new Intent (intent.action_send); Intent.settype ("image/*"); Intent.putextra (Intent.extra_stream, URI); if (subject! = NULL &&! "". Equals (subject)) {Intent.putextra (intent.extra_subject, subject);} if (content! = NULL &&! "". Equals (content)) {Intent.putextra (intent.extra_text, content);} Set the popover caption if (dlgtitle! = null &&! "). Equals (Dlgtitle)) {//Custom title startactivity (Intent.createchooser (Intent, Dlgtitle));} else {//system default caption StartActivity ( Intent);}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true;}}

Among them, the Sharetext method realizes the function of sharing the text content, and the Shareimg method realizes the function of sharing the picture. Intent.extra_stream can also share other types of resource files, such as: MP3.


The effect of the operation is as follows:

1. Share text content to






2. Share text content to QQ


3. Share images to




4. Share pictures to QQ




Android uses intent.action_send for simple sharing functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.