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