"Android" 19.2 Shareactionprovider class-to help you share information

Source: Internet
Author: User

Category: C #, Android, VS2015;

Date Created: 2016-03-06 I. INTRODUCTION

The shared Operations provider Class (Shareactionprovider) simplifies the operation of strings, pictures, icons, and other types of binaries that you want to share or share with others (or other applications). In other words, it helps you achieve features like our common "share to Weibo", "share to QQ space", "Share by SMS", "Share by email" 、......, and share in a module or area of your own application (such as the information that is presented in the Action Bar Actionbar).

In short, Shareactionprovider is helping you to share information with any one or more of the goals you want to share, as long as those goals meet the actions you specify in the sharing feature, these goals will automatically receive the information you share.

Speaking of which, you should also understand further the meaning or purpose of another layer of the Sharedpreferenc class just introduced in the previous section, that is, in addition to saving information such as application configuration, you can save the information that your favorite people or other programs share, so it is called "shared operations preferences."

1. Types of information to share

The "information" that is shared, whatever it is, belongs to one of the following two categories.

(1) string sharing (or text message sharing)

What's the use of string sharing? For example, someone wants you to do something like this: When several people go out for a trip, these people want to know where other people are now. The "other person's current location" is actually a shared string, which can be a latitude or longitude, or an address that can be called a name.

If this string and Baidu map together, you can easily achieve just said "a few people out of the travel" described in the function. There seems to be a software called "Walk with You", the software should be based on this principle to achieve. Don't forget, the 3rd chapter you have learned the Baidu Map API provides "short string sharing" and "Map callout" function, this "short string" is the string mentioned here. If the string is longitude and latitude, then you can add a "callout" to the latitude and longitude position of the Baidu map. If the location of a few people at the same time marked out, these few people can be intuitive to see where the other several people, hehe.

Further expansion, you can also achieve a similar "monitoring the elderly out of the current location", "monitoring the current location of the taxi" 、...... such as

If you combine strings with your application modules, each module that satisfies the filtering criteria can automatically receive this information.

(2) Binary stream sharing

We know that any binary file can be accessed through a stream, so what else can you not share it with? In other words, if you want to share, what information can be shared.

2. Type of operation to share

The essential meaning of "sharing" is that you need to send the information you are ready to share in order to receive the target that meets the filtering criteria.

Common options for sharing operations available with Android are:

Android.Content.Intent.ActionSend

Android.Content.Intent.ActionSendMultiple

3. Who receives the shared information (filter conditions)

The goal of satisfying the filter is to designate the receiver itself as needed, such as in the program through the Intentfilter feature to declare the filter condition, that is, what other people or other programs can receive information that is shared.

If someone else is using the program to share the information, and your program is unwilling to receive the shared information, then you will not be receiving it (SMS, email, it is a passive reception, unless you filter out the text messages or messages, such as spam, spam, etc.).

These are the 3 key points of Shareactionprovider, and the following examples demonstrate basic usage. The basic usage of example 19-2--shareactionprovider

This example shows how to share the image with Shareactionprovider in the menu of the action bar, as the string sharing is relatively simple and is not demonstrated here.

When the user clicks the Image Sharing menu item on the Action Bar, Shareactionprovider automatically loads the indent associated with it and then shares the image.

This example uses the messaging application described earlier, and the goal of accepting shared information is to use it to receive shared images. In addition, this example demonstrates how to read an image file (Monkey.png) from the assets folder to a shared storage area.

In the Ch1902MainActivity.cs file, copy the image (Ch1902monkey.png) to a location where you can share it, and then use intent to share it.

In the Ch1902SharedShow.cs file, the image that is shared in the Ch1902MainActivity.cs file can be displayed in the interface by Intentfilter specifying the filter that satisfies the receive share.

Run

The left image below is the start interface (Ch1902main.axml), and the right image is the menu item interface (Ch1902actionbarmenu.axml) that pops up by clicking "...".

The left-hand image below is a menu item that appears after clicking Picture Sharing, which is automatically found by the system. The "Picture Sharing Viewer" is a menu item that is automatically found by the system when it is specified in the Ch1902SharedShow.cs file to meet the conditions for sharing filtering, and the right image is when you click the Picture Sharing Viewer menu item. The shared results are automatically displayed in the Ch1902sharedshow.axml file.

Design steps

1. Place a picture under the assets

Drag and drop the Ch1902monkey.png into the assets folder. This step is primarily to demonstrate how to read the image resources under the assets folder.

2. Add menu (Ch1902actionbarmenu.xml)

Create the file under the Resources/menu subfolder.

There is only one menu item in this menu, in which the Android:actionproviderclass is set to "Android.widget.ShareActionProvider", and it automatically creates an instance of the class. This allows you to reference this instance in the Ch1902MainActivity.cs file.

<?XML version= "1.0" encoding= "Utf-8"?> <Menuxmlns:android= "Http://schemas.android.com/apk/res/android">  <ItemAndroid:id= "@+id/ch1902_overflowmenuitem"android:showasaction= "Never"Android:title= "Picture sharing"Android:actionproviderclass= "Android.widget.ShareActionProvider" /></Menu>

3. Add Ch1902main.axml

Add the file under Resources/layout.

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent">    <TextViewAndroid:text= "Tip: Click the" ... "menu in the upper-right corner. "Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"android:gravity= "Center"Android:layout_margintop= "30DP"Android:id= "@+id/ch1902_textview1" /></LinearLayout>

4. Add Ch1902MainActivity.cs

Add the file under the Srcdemos folder.

To display menus under the menu subfolder (ch1902actionbarmenu.xml), you need to override the Oncreateoptionsmenu () method in the Ch1902mainactivity class, and once you have a reference to the menu, You can get the Shareactionprovider object from the Actionprovider property of the menu item, and then call the object's Setshareintent () method to set the shareable intent.

In the parameters of the Setshareintent () method of the Shareactionprovider object, specify the shared intent.

usingSystem.IO;usingAndroid.app;usingandroid.content;usingAndroid.os;usingandroid.views;usingAndroid.widget;namespacemydemos.srcdemos{[Intentfilter (New[]{Intent.actionmain}, Categories =New[] {ch. Mydemoscategory})] [Activity (Label="example 19-2-sharing basic usage")]     Public classch1902mainactivity:activity {Private stringFilename="Ch1902monkey.png";//shared picture file name        protected Override voidOnCreate (Bundle savedinstancestate) {Base.            OnCreate (savedinstancestate);  Copytopublic (FileName); //to read an image resource under assets to a shared storage areaSetcontentview (Resource.Layout.ch1902Main); }         Public Override BOOLoncreateoptionsmenu (Imenu menu) {//Fill Menumenuinflater.inflate (Resource.Menu.ch1902ActionBarMenu, Menu); varitem =menu.            FindItem (Resource.Id.ch1902_overflowMenuItem); varProvider =(Shareactionprovider) item.            Actionprovider; varPictureintent =NewIntent (intent.actionsend); Pictureintent.settype ("image/*"); varURI =Android.Net.Uri.FromFile (Getfilestreampath (fileName));            Pictureintent.putextra (Intent.extrastream, URI); //set up a shared intentprovider.            Setshareintent (pictureintent); return true; }        Private voidCopytopublic (stringfileName) {            using(Stream FromStream =Assets.open (FileName)) {                stringFilePath = Path.Combine (New string[] {"Data","Data", PackageName,"Files", fileName}); intSize = +*1024x768; using(FileStream Tostream =NewFileStream (FilePath, FileMode.Create, FileAccess.ReadWrite)) {                    intn =-1; byte[] buffer =New byte[size];  while(n = fromstream.read (buffer,0, size)) >0) {tostream.write (buffer,0, N); }                }            }        }    }}

5. Add Ch1902sharedshow.axml

Add the file under Resources/layout.

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent">    <TextViewAndroid:text= "The image below is from picture sharing"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"android:gravity= "Center"Android:layout_margintop= "30DP"Android:id= "@+id/ch1902_textviewshow" />    <ImageViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/ch1902_imageviewshow"Android:layout_margintop= "30DP" /></LinearLayout>

6. Add Ch1902SharedShow.cs

Add the file under the Srcdemos folder.

This file is primarily intended to demonstrate how to set the filter conditions for receiving picture sharing.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingAndroid.app;usingandroid.content;usingAndroid.os;usingAndroid.widget;namespacemydemos.srcdemos{[Intentfilter (New[]{Intent.actionsend}, Categories=New[] {intent.categorydefault}, Datamimetype="image/*")] [Activity (Label="Picture Sharing viewer")]     Public classch1902sharedshow:activity {protected Override voidOnCreate (Bundle savedinstancestate) {Base.            OnCreate (savedinstancestate);            Setcontentview (Resource.Layout.ch1902SharedShow); varImageuri =(Android.Net.Uri) Intent.getparcelableextra (Intent.extrastream); if(Imageuri! =NULL)            {                varImageView1 = findviewbyid<imageview>(Resource.Id.ch1902_imageViewShow);            Imageview1.setimageuri (Imageuri); }        }    }}

"Android" 19.2 Shareactionprovider class-to help you share information

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.