Android uses the system intent to implement the sharing Function and add the application to the sharing list ++ share email.

Source: Internet
Author: User

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.

  1. // Cachedir is the directory of the file object to be shared.
  2. // You can overwrite file F with your own file object
  3. File cachedir = new file (Android. OS. environment. getexternalstoragedirectory (), getstring (getapplicationinfo (). labelres ));
  4. File F = new file (cachedir, "image_name.jpg ");
  5.  
  6. Intent intent = new intent (intent. action_send );
  7. Intent. settype ("image/JPEG ");
  8. Intent. putextra (intent. extra_text, "email body over here ");
  9. Intent. putextra (intent. extra_subject, "Email Subject over here ");
  10. Intent. putextra (intent. extra_stream, Uri. fromfile (f ));
  11. Startactivity (intent. createchooser (intent, "share :"));

 

Related Article

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.