A brief introduction to sharing sending and receiving of app text content in Android

Source: Internet
Author: User

Remember (specify selector intent.createchooser ())

Before starting today's content, let's chat:

(1) Suddenly one day brainstorming, a lot of problems have a new view and insight, eager to share to everyone, the document has been written, I need to pass or QQ, SMS and other social tools to send to everyone.

(2) Find a very good article on the internet, want to save the collection.

The above describes the two situations that we often encounter when we enter the smartphone era, so how do we, as developers, enable our own apps to implement both of these functions? Below we can realize the implementation of the two functions by sending and sharing the text and receiving it (other types of data at the end of the blog will give you a brief introduction).

The first case: app to send share text function

When implementing the app send and share feature, there are two different implementations depending on whether the selector intent.createchooser ()is specified.

(1) The implementation effect of the specified selector is as follows:

Every time you need to send a share, all the apps with the sharing feature will pop up to choose from. (Personal opinion is very humane)

(2) The implementation effect of unspecified selector is as follows:

The demo test uses the Android native System , when the selector is not set, each time the user will be reminded to use the current app submission send sharing app use only once or always use (tested in case everyone is slipping, delayed "always", then well, If you want to share content with another app next time, unless you unload the current app, but when you test on some other Android -branded phones, you'll only see the first time you're out of the app with the Send sharing feature (but you won't be prompted to use it only once or always). Once selected, the result is the same as clicking on the native system always. Immediately uninstall the app Heart has.

Well, the implementation of the effect everyone saw, we began to masturbate a code it:

The first step: Layout file Activity_main.xml file (text edit box and button):
1<?xml version= "1.0" encoding= "Utf-8"?>2<LinearLayout3Xmlns:android= "Http://schemas.android.com/apk/res/android"4Xmlns:tools= "Http://schemas.android.com/tools"5Android:id= "@+id/activity_main"6Android:layout_width= "Match_parent"7android:layout_height= "Match_parent"8android:orientation= "Vertical"9tools:context= "Com.example.administrator.sendshare.MainActivity" >Ten<EditText OneAndroid:id= "@+id/et" AAndroid:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -Android:hint= "Want to say something to Pan"/> the<Button -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -android:onclick= "SendText" +android:text= "Send Text"/> -</LinearLayout>
Step Two: Implementing code Mainactivity.java in Java implements code:

Note: Specify the selector.

1  Public classMainactivityextendsappcompatactivity {2EditText et;//Declaration text edit Box3String str;//declares a string that gets the contents of the text edit box4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8         //Get text Box9ET =(EditText) Findviewbyid (r.id.et);Ten     } One     //the creation method sends out the input A      Public voidsendtext (view view) { -Str=Et.gettext (). toString (); -Intent Intent =NewIntent (); the         /*set action to send share, - * and determine if the content to be sent is empty -          */ - intent.setaction (intent.action_send); +         if(str!=NULL){ - Intent.putextra (INTENT.EXTRA_TEXT,STR); +}Else{ AIntent.putextra (Intent.extra_text, ""); at         } -Intent.settype ("Text/plain");//set the type of data sent for sharing -         //No selector is specified, and after the first selection of some custom systems, the latter cannot be changed again - //startactivity (intent); -         //specifies that the selector chooses to use the app with the Send text feature - startactivity (Intent.createchooser (Intent,getresources () GetText (r.string.app_name))); in     } -}
Second case: The app receives the shared text function

The effect is as follows (share SMS content to our app):

Layout interface for the initial default, only a default HelloWorld TextView interface, here is omitted to write.

The first step: androidmainfest.xml configuration file (add intent properties such as action required to receive text)
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3      Package= "Com.example.administrator.test" >4<Application5Android:allowbackup= "true"6android:icon= "@mipmap/ic_launcher"7Android:label= "@string/app_name"8Android:supportsrtl= "true"9Android:theme= "@style/apptheme" >Ten<activity android:name= ". Mainactivity "> One<intent-filter> A<action android:name= "Android.intent.action.MAIN"/> -                 //add Action,category,mimetype for receiving text -<action android:name= "Android.intent.action.SEND"/> the<category android:name= "Android.intent.category.DEFAULT"/> -<data android:mimetype= "Text/plain"/> -<category android:name= "Android.intent.category.LAUNCHER"/> -</intent-filter> +</activity> -</application> +</manifest>
Step Two: Implementing code in Java implementation code Mainactivity.java function
1 Importandroid.content.Intent;2 Importandroid.support.v7.app.AppCompatActivity;3 ImportAndroid.os.Bundle;4 ImportAndroid.widget.TextView;5  Public classMainactivityextendsappcompatactivity {6     PrivateTextView TV;7 @Override8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); OneTV =(TextView) Findviewbyid (r.id.tv); A         //Get Intent -Intent Intent =getintent (); -String action =intent.getaction (); theString type =Intent.gettype (); -         //set the receive type to text -         if(Intent.ACTION_SEND.equals (ACTION) && type! =NULL){ -             if("Text/plain". Equals (Type) { + Handlertext (intent); -             } +         } A } at //This method is used to obtain the text information contained in the intent and display it to the app's activity interface . -     Private voidHandlertext (Intent Intent) { -String data =Intent.getstringextra (intent.extra_text); - tv.settext (data); -     } -}
Additional additions: Set the update desktop background with the following core code:
1 Importandroid.content.Intent;2 Importandroid.support.v7.app.AppCompatActivity;3 ImportAndroid.os.Bundle;4 ImportAndroid.view.View;5  Public classMain2activityextendsappcompatactivity {6 @Override7     protected voidonCreate (Bundle savedinstancestate) {8         Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main2);Ten     } One      Public voidSelect (view view) { AIntent Intent =NewIntent (); - intent.setaction (intent.action_set_wallpaper); - startactivity (Intent.createchooser (Intent,getresources () GetText (r.string.app_name))); the //startactivity (intent); -     } -}
Share the sending picture in binary form, the core code is as follows:
1  Public voidsendimage (view view) {2Intent Intent =NewIntent ();3 intent.setaction (intent.action_send);4Intent.putextra (Intent.extra_stream, Uri.parse (environment.getexternalstoragedirectory () + "/DCIM/Camera/20161204 _195228.jpg "));5Intent.settype ("image/*");6 //startactivity (intent);7 startactivity (Intent.createchooser (Intent,getresources () GetText (r.string.app_name)));8}
Send to share multiple images, the core code is as follows:
1  Public voidsendimages (view view) {2arraylist<uri> URIs =NewArraylist<>();3     //demo send two pictures4Uris.add (Uri.parse (environment.getexternalstoragedirectory () + "/dcim/camera/20161204_195228.jpg"));5Uris.add (Uri.parse (environment.getexternalstoragedirectory () + "/dcim/camera/20161204_195155.jpg"));6Intent Intent =NewIntent ();7 intent.setaction (intent.action_send_multiple);8 Intent.putparcelablearraylistextra (intent.extra_stream,uris);9Intent.settype ("image/*");Ten //startactivity (intent); One startactivity (Intent.createchooser (Intent,getresources () GetText (r.string.app_name))); A}


We are here today, I have a question to welcome you to discuss, good night!

A brief introduction to sharing sending and receiving of app text content in Android

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.