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

Source: Internet
Author: User
Tags gettext xmlns

Remember (designate selector Intent.createchooser ())

Before starting today's content, let's have a little chat:

(1) Suddenly one day brainstorming, a lot of questions have a new view and opinion, can't wait to share to everyone, the document has been written, I need to pass micro-mail 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 down.

It describes two things that we often encounter when we enter the smartphone age, so how do we, as developers, make the apps that we develop to achieve both of these functions? Here's how we can get the text to be shared and received to comb through the implementation of the next two functions (other types of data are briefly introduced at the end of the blog post).

The first case: app implementation send share text function

When implementing the function of app sending and sharing, there are two different implementation effects depending on whether or not 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 of the apps that have a share will pop up. (personally considered very humane)

(2) The implementation effect of not specifying a selector is as follows:

The diagram shows the Android native system used in the test, when the selector is not set, each time the user is reminded to use the current app for sending and sharing the app is used only once or always (tested in case everyone is slipping, late "always", well, if next time you want to share content with other apps, Unless you unload the current app, but when you test on other Android branded handsets, discover that only the first time you will be able to jump out of all the apps that have the ability to send and share features (but you won't be prompted to use them once or always), once selected, the consequences are the same as if you clicked on a native system. Immediately uninstall the app's heart.

Well, the implementation of the effect we all see, we began to masturbate a code bar:

Step one: Layout interface layout file Activity_main.xml file (text edit box and button):

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/" Apk/res/android "
 xmlns:tools=" Http://schemas.android.com/tools "
 android:id=" @+id/activity_main
 " Android:layout_width= "Match_parent"
 android:layout_height= "match_parent"
 android:orientation= "vertical" "
tools:context=" com.example.administrator.sendshare.MainActivity ">
 <edittext
 android:id=" @+id/et "
 android:layout_width=" match_parent "
 android:layout_height=" wrap_content "
 android:hint=" "Want to say something to Pan"/>
 <button
 android:layout_width= match_parent "android:layout_height=" Wrap_
 Content "
 android:onclick=" SendText "
 android:text= send Text"/>
</LinearLayout>

The second step: Java implementation code Mainactivity.java function implementation code:

Note: Specifies the selector.

public class Mainactivity extends Appcompatactivity {
 edittext et;//Declaration text edit box
 string str;//declaration string for getting contents of text edit box
 @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);
 Gets the text box
 et = (edittext) Findviewbyid (r.id.et);
 }
 The creation method emits the input to the public
 void SendText (view view) {
 str=et.gettext (). toString ();
 Intent Intent = new Intent ();
 /* Set ACTION to send share,
 * and determine whether the content to be sent to share is empty
 /intent.setaction (intent.action_send);
 if (str!=null) {
  Intent.putextra (INTENT.EXTRA_TEXT,STR);
 } else{
  Intent.putextra (Intent.extra_text, "");
 }
 Intent.settype ("Text/plain");/set share sent data type
 //unspecified selector, partial customization system after first selection, later will not be able to change again
//startactivity (intent);
 Specifies that the selector chooses to use the app StartActivity (
 intent,getresources () Intent.createchooser () with the Send Text feature. GetText (r.string.app_name )));
 }
}

The second scenario: app implementation receives shared text functionality

The results are as follows (sharing SMS content to our app):

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

Step One: Androidmainfest.xml configuration file (add intent properties required to receive text)

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 package=" com.example.administrator.test >
 <application
 android:allowbackup= "true"
 android:icon= "@mipmap/ic_launcher"
 android:label= "@string/app_name"
 android:supportsrtl= "true"
 android:theme= "@style/apptheme" >
 <activity android:name= ". Mainactivity ">
  <intent-filter>
  <action android:name=" Android.intent.action.MAIN "/>
  //Add received text for Action,category,mimetype
  <action android:name= "Android.intent.action.SEND"/>
  <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: Implement code Mainactivity.java function implementation code in Java

Import android.content.Intent;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Mainactivity extends Appcompatactivity {
 private TextView TV;
 @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);
 TV = (TextView) Findviewbyid (r.id.tv);
 Obtain Intent
 Intent Intent =getintent ();
 String action = Intent.getaction ();
 String type = Intent.gettype ();
 Sets the receive type to text
 if (Intent.ACTION_SEND.equals (ACTION) && type!= null) {
  if ("Text/plain". Equals (Type)) {
  Handlertext (intent);
}}} This method is used to get the textual information contained in Intent and to display the
 private void Handlertext (Intent Intent) {
 String data on the app's activity interface Intent.getstringextra (intent.extra_text);
 Tv.settext (data);
 }

Additional additions:

Settings Update desktop background, the core code is as follows:

Import android.content.Intent;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
public class Main2activity extends Appcompatactivity {
 @Override
 protected void OnCreate (Bundle Savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main2);
 }
 public void Select (View view) {
 Intent Intent = new Intent ();
 Intent.setaction (intent.action_set_wallpaper);
 StartActivity (Intent.createchooser (Intent,getresources (). GetText (R.string.app_name)));
StartActivity (intent);
 }

To share the sending picture in binary form, the core code is as follows:

public void Sendimage (view view) {
 Intent Intent = new Intent ();
 Intent.setaction (intent.action_send);
 Intent.putextra (Intent.extra_stream, Uri.parse (environment.getexternalstoragedirectory () + "/DCIM/Camera/20161204 _195228.jpg "));
 Intent.settype ("image/*");
StartActivity (intent);
 StartActivity (Intent.createchooser (Intent,getresources (). GetText (R.string.app_name)));

Send and share multiple pictures, the core code is as follows:

public void Sendimages (view view) {
 arraylist<uri> URIs = new arraylist<> ();
 Demo send two pictures
 uris.add (Uri.parse (environment.getexternalstoragedirectory () + "/dcim/camera/20161204_195228.jpg") ));
 Uris.add (Uri.parse (environment.getexternalstoragedirectory () + "/dcim/camera/20161204_195155.jpg"));
 Intent Intent = new Intent ();
 Intent.setaction (intent.action_send_multiple);
 Intent.putparcelablearraylistextra (Intent.extra_stream,uris);
 Intent.settype ("image/*");
StartActivity (intent);
 StartActivity (Intent.createchooser (Intent,getresources (). GetText (R.string.app_name)));

Come here today, have a question welcome comment discussion, good night!

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support 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.