How can I add the sharing function to an application and add the application to the sharing selection list of the system?
Intent. createchooser ()Method to bring up the system share list.
Check whether the corresponding intent component exists. You can check android to determine whether intent exists and whether intent is available.
1. added the sharing Function for applications.
Import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class fenxiangactivity extends activity {private button btnfenxiang = NULL;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btnfenxiang = (button) findviewbyid (R. id. btnfenxiang); btnfenxiang. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {intent = new intent (intent. action_send); // enable the intent attribute for sharing and sending. settype ("text/plain"); // share the data type intent sent. putextra (intent. extra_subject, "subject"); // share the topic intent. putextra (intent. extra_text, "extratext"); // shared content // intent. setflags (intent. flag_activity_new_task); // This may be the background of the shared list. fenxiangactivity. this. startactivity (intent. createchooser (intent, "share"); // the title of the target application selection dialog box }});}
The above code is shared text. If you want to share image information, you need to set settype to "image/*" and pass the intent. extra_stream parameter of the URI type.
2. Add an application to the system share list
You only need to add the following code to androidmanifest. xml:
1 2 3 4 5 6 7 |
<Activity Android: Name = ". sharepage" Android: Label = "share to Weibo"> <Intent-filter> <Action Android: Name = "android. Intent. Action. Send"/> <Category Android: Name = "android. Intent. Category. Default"/> <Data Android: mimetype = "image/*"/> </Intent-filter> </Activity> |
Download address: http://files.cnblogs.com/firecode/Fenxiang.rar
Android: Implementation of "share to email"
This is a simple implementation of sharing to the mailbox, you can attach your own picture.
- // Cachedir is the directory of the file object to be shared.
- // You can overwrite file F with your own file object
- File cachedir = new file (Android. OS. environment. getexternalstoragedirectory (), getstring (getapplicationinfo (). labelres ));
- File F = new file (cachedir, "image_name.jpg ");
-
- Intent intent = new intent (intent. action_send );
- Intent. settype ("image/JPEG ");
- Intent. putextra (intent. extra_text, "email body over here ");
- Intent. putextra (intent. extra_subject, "Email Subject over here ");
- Intent. putextra (intent. extra_stream, Uri. fromfile (f ));
- Startactivity (intent. createchooser (intent, "share :"));