Using and implementing the system "sharing" interface instance _android in Android applications

Source: Internet
Author: User
Tags home screen

In order to apply the promotion, dissemination, many applications have "sharing" function, a button, click will appear after the text message, microblogging and so on all realized the sharing function of the application list. This article mainly describes how to invoke the sharing function and how to implement the sharing interface so that it can be used in the share list. Android apps are a great place to do these things, and that's the big thing about Android, and it's easy to get through the communication between applications.

Call sharing function

1. Share text

Sharing function uses an implicit start activity method, where the ACTION uses the action_send.

Intent sendintent = new Intent (); 
Sendintent.setaction (intent.action_send); 
Sendintent.putextra (Intent.extra_text, "This is me TEXT to send."); 
Sendintent.settype ("Text/plain"); 

The effect is as shown in figure one.

2. Change share list title

Share the list titled "Use Content Complete" with the above sharing method, and Intent.createchooser () is available in Android, which allows you to consistently display the share selection list and modify the share list header content.

Intent sendintent = new Intent (); 
Sendintent.setaction (intent.action_send); 
Sendintent.putextra (Intent.extra_text, "This is me TEXT to send."); 
Sendintent.settype ("Text/plain"); 
StartActivity (Intent.createchooser (Sendintent, Getresources (). GetText (r.string.send_to))); 

Benefits of using Intent.createchooser ():

If you are Callintent.createchooser () for the intent, Android would always display the Chooser. This has some advantages:

    • Even if the user has previously selected a default action for this intent, the Chooser would still be displayed.
    • If no Applications match, Android displays a system message.
    • Can specify a title for the Chooser dialog.

The sharing function is not only intent.extra_text, but also Extra_email, EXTRA_CC, EXTRA_BCC, Extra_subject. Only the receiving party is required to complete the response data acceptance.

3, share the picture

The sharing feature also supports binary content (Binary), but most of the pictures are processed because Shareintent.settype ("Image/jpeg") sets the content type. can also be other types, which require receiving side support.

Intent shareintent = new Intent (); 
Shareintent.setaction (intent.action_send); 
Shareintent.putextra (Intent.extra_stream, uritoimage); 
Shareintent.settype ("Image/jpeg"); 

4, share the picture list

The sharing function not only supports the single picture, but also supports the picture list, here or the scope is too narrow, should declare not only the picture.

arraylist<uri> Imageuris = new arraylist<uri> (); 
Imageuris.add (IMAGEURI1); Add your image URIs here 
imageuris.add (IMAGEURI2); 
 
Intent shareintent = new Intent (); 
Shareintent.setaction (intent.action_send_multiple); 
Shareintent.putparcelablearraylistextra (Intent.extra_stream, imageuris); 
Shareintent.settype ("image/*"); 

Realize sharing functionality

The above is all how to invoke the sharing function, the following starts to write how to realize the sharing function, let our application also appear in the Share list. It is also mentioned that the sharing function is implemented using implicit invocation activtiy, and the activity needs to declare <intent-filter>.

Statement Intent-filter

 <activity android:name= "Com.example.sharedemo.ShareActivity" Android:lab El= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.SEND"/> 
 
        ; 
      <category android:name= "Android.intent.category.DEFAULT"/> <data android:mimetype= "image/*"/> </intent-filter> <intent-filter> <action android:name= "Android.intent.action.SEND"/> 
 
        ; 
      <category android:name= "Android.intent.category.DEFAULT"/> <data android:mimetype= "Text/plain"/> </intent-filter> <intent-filter> <action android:name= "Android.intent.action.SEND_MU Ltiple "/> <category android:name=" Android.intent.category.DEFAULT "/> <data Android:mim Etype= "image/*"/> </intent-filter> </activity> 

It says three kinds of intent-filter, of course, more, just for example,

Processing receive data
The Intent-filter is declared, and the response activity is processed to handle the data of the response, as follows:

public class Shareactivity extends activity{@Override protected void onCreate (Bundle savedinstancestate) { 
     
    TODO auto-generated Method Stub super.oncreate (savedinstancestate); 
    Get intent, action and MIME type Intent intent = getintent (); 
    String action = Intent.getaction (); 
 
    String type = Intent.gettype (); if (Intent.ACTION_SEND.equals (ACTION) && type!= null) {if ("Text/plain". Equals (Type)) {Handlese Ndtext (Intent); Handle text being sent} else if (Type.startswith ("image/")) {handlesendimage (intent);//Handle Singl E-image being sent}} else if (Intent.ACTION_SEND_MULTIPLE.equals (ACTION) && type!= null) {I 
    F (Type.startswith ("image/")) {handlesendmultipleimages (intent);//Handle multiple-images being sent} else {//Handle intents, such as being started from the ' Home screen}} void Handlesendte XT (IntentIntent) {String Sharedtext = Intent.getstringextra (Intent.extra_text); 
    String sharedtitle = Intent.getstringextra (Intent.extra_title); if (Sharedtext!= null) {//Update UI to reflect text being shared}} void Handlesendimage (Intent in 
    Tent) {uri Imageuri = (URI) Intent.getparcelableextra (Intent.extra_stream); if (Imageuri!= null) {//Update UI to reflect image being shared}} void Handlesendmultipleimages (I 
    Ntent Intent) {arraylist<uri> Imageuris = Intent.getparcelablearraylistextra (Intent.extra_stream); 
 if (Imageuris!= null) {//Update UI to reflect multiple images shared}}}

By declaring the Intent-filter, processing the received data can complete the sharing reception function.

More

The above only makes the sharing function simple explanation, along with the Android API upgrade, also appeared some new completes "the sharing" the function the method, for instance Shareactionprovider, more please refer.

Demo Download: Demo

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.