Explain the method of using intent class to implement the call between components in Android _android

Source: Internet
Author: User
Tags int size serialization

Intent is a class used in Android to invoke other components, and through intent we can easily invoke Activity,broadcast receiver and service.

Intent Intent = new Intent (intent.action_view);
Intent.setdata (Uri.parse ("http://www.baidu.com"));
StartActivity (Intent);

This code can be used to invoke third party activity (start a third party browser to open the Baidu homepage).
Intent have implicit and explicit points, above the

Intent Intent = new Intent (Intent.action_view);

The intent created is called an implicit intent. Building an implicit intent requires a string representing the ACTION (for example, Intent.action_view, whose value is "Android.intent.action.VIEW"), Android looks for an activity that can handle the action (declared in the Intent-filter under the activity in the manifest file) and calls him.
It is sometimes possible for multiple activity to declare that it can handle an action, for example:

<activity
  android:name= ". Activity1 ">
  <intent-filter>
    <action android:name=" Com.abc.def "/>
    <category Android : Name= "Android.intent.category.DEFAULT"/>
  </intent-filter>
</activity>
<activity
  android:name= ". Activity2 ">
  <intent-filter>
    <action android:name=" Com.abc.def "/>
    <category Android:name= "Android.intent.category.DEFAULT"/>
  </intent-filter>
</activity>

The above Activity1 and Activity2 both declare that they can handle the "com.abc.def" action, so when we execute the following code

Intent Intent = new Intent ("Com.abc.def");
StartActivity (Intent);

Activity1 and Activity2 both meet the requirements, Android will pop up the "Complete Action Using" dialog box to allow users to select an activity to perform.

It is noteworthy that to be able to match an implicit intent invocation, you must include the category of DEFAULT (that is, <category android:name= "Android.intent.category.DEFAULT"/ >), and to match an explicit intent, you do not need the category.

For implicit intent, in addition to the action, there are a variety of information available to help Android choose the best match. Additional information that can be added includes: Host,mimetype,path,pathpattern,pathprefix,port,scheme.

For example, add a mimetype attribute for the configuration above Activity2 in manifest:

<activity
  android:name= ". Activity2 ">
  <intent-filter>
    <action android:name=" Com.abc.def "/>
    <category Android : Name= "Android.intent.category.DEFAULT"/>
    <data android:mimetype= "Abc/def"/>
  </ Intent-filter>
</activity>

So:

Intent Intent = new Intent ("Com.abc.def");
StartActivity (Intent)//only Activity1 conforms to

/********************************************/
Intent Intent = New Intent ("Com.abc.def");
Intent.settype ("Abc/def");
StartActivity (intent);/only Activity2 match

If you specify a class to invoke (such as the new Intent (Xxactivity.this, Xx.class), or by SetComponent, when you create a Intent, this Intent is called the display Intent.

For explicit intent, because he has indicated the specific class to invoke, Android ignores its action,category and the Data property. (Personally feel that the display of intent calls is faster than implicit)

Serializable vs Parcelable
Android is mainly through the intent to achieve mutual invocation between components, but also to pass additional data. This data is primarily stored in Bundle (when Intent.putextras (Bundle) is invoked, Android copies the data in Bundle instead of referencing, so modifying the data in the Bundle does not change the data that is carried in intent.

Bundle can contain basic data types and classes that implement serializable or parcelable interfaces.

When we want to store a class obj (a simple class containing two int members) to the bundle, you can let it implement the serializable or Parcelable interface as follows:

1.Serializable


public class OBJ implements Serializable {
  private int A;
  private int B;
  Public OBJ (int a, int b) {
    this.a = A;
    this.b = b;
  }
  
  @Override public
  String toString () {return
    "OBJ: [" + A + "," + B + "]";
  }
}

We can use Intent.putextra ("obj", new obj (1, 2)) to put it in the intent and then take it out by obj = (obj) intent.getserializableextra ("obj").
2.Parcelable


 public class Objpar implements parcelable {private int A;
  private int B;
    Public Objpar (int a, int b) {this.a = A;
  this.b = b;
  @Override public String toString () {return "OBJ: [" + A + "," + B + "]";
  @Override public int describecontents () {return 0;
    @Override public void Writetoparcel (Parcel dest, int flags) {dest.writeint (a);
  Dest.writeint (b);
    public static final parcelable.creator<objpar> Creator = new creator<objpar> () {@Override
    Public objpar[] NewArray (int size) {return new objpar[size]; @Override public Objpar Createfromparcel (Parcel source) {return new Objpar (Source.readint ().
    Source.readint ());
}
  }; }

We can use Intent.putextra ("Objpar", New Objpar (1, 2)) to put it in intent, and then through Objpar = (objpar) Intent.getparcelableextra (" Objpar "); To take it out.
These are two methods of storing object objects in bundle, and it is obvious that it is simpler to implement the serializable interface because he is a tagged interface and does not need to implement a specific method. The implementation of the Parcelable interface is relatively complex, but the benefit is a significant increase in performance. This is because when we implement the serializable interface, the real serialization work is done by the JDK, and he needs to get the member variables by reflection, because the performance of the reflection is not high, so the serialization is slow. When implementing the Parcelable interface, however, we provide implementations of the defined methods in the interface (Writetoparcel implementation serialization, Createfromparcel implementation deserialization), which avoids the use of reflection, so it is fast.

So what are the differences in performance between these two approaches? Here is a test result on a foreign website: Serializable time is about 10 times times the parcelable

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.