Android Development Art Exploration (study note) IPC mechanism in--03-android (i)

Source: Internet
Author: User

Android Development Art Exploration (study notes)

Dimon

  • Weibo: @Dimon-Kuu
  • GitHub: @Dimon94
  • Lofter: @Dimon,
IPC mechanism in 03-android (i) Introduction of 1.Android IPC

IPC (inter-process-communication): means inter-process communication, which refers to the process of data exchange between two processes.

What is a process: generally refers to an execution unit, a program or an application on a PC and a mobile device.
What is a thread: The thread is the smallest unit of CPU scheduling and is a limited system resource.

And a process can contain multiple threads.

Multi-process mode in 2.Android

android:processyou can turn on multi-process mode by specifying properties for the four components. (unconventional method, JNI native go to fork a new process in the layer)

//没有定义默认进程的进程名为包名:"com.dimon"android:process=":remote"  ->  进程名:"com.dimon:remote" 用":"开头的进程属于当前应用的私有进程android:process="com.dimon.remote"  ->  进程名:"com.dimon.remote" 全局进程

Private process: The components of other applications cannot be run in the same process as it.

Global processes: Other apps ShareUID UID UID can run in the same process as each app by having its own unique app to share data with. Multiple processes across the same application can be understood as two different applications using the SHAREUID model.

In addition to the DDMS view view process, it can also be used shell to view, the command is: adb shell ps or adb shell ps|grep com.dimon ( com.dimon is the package name)

Problems caused by multiple processes
    • Static members and Singleton modes are completely invalidated: the main is that each process is assigned a separate virtual machine, different virtual machines have different address space in memory allocation;
    • The thread synchronization mechanism completely fails: because it is not the same block of memory, thread synchronization cannot be guaranteed;
    • SharePreferencesReliability degradation: The SharePreferences underlying is implemented by a read/write XML file, concurrent write may be problematic, that is, it does not support two processes simultaneously to perform write operations;
    • Applicationis created multiple times
Serializable interface

SerializableIs Java a serialized interface that is provided.

public  class  user  implements  serializable  { private  static  final  long  serialversionuid = 519067123721295773  L;< Span class= "hljs-comment" >//deserialization when the system detects whether the version is the same, the same can be counted successfully deserialized, not the same will deserialize the failure of  public   int  userId; public   String Uesrname; public   boolean  Ismale; ...}
//序列化过程new User(0"jake"truenew ObjectOutputStream(  new FileOutputStream("cache.txt"));out.writeObject(user);out.close();//反序列化过程new ObjectInputStream(  new FileInputStream("cache.txt"));User newUser = (User) in.readObject();in.close();
    • Static member variables that belong to a class do not belong to the object, so they do not participate in the serialization process;
    • transientmember variables marked with keywords do not participate in the serialization process;
Parcelable interface

Typical usage:

 Public  class User implements parcelable {   Public intUesrid; PublicString Uesrname; Public BooleanIsmale; PublicBook book; Public User(intUserId, String Uesrname,BooleanIsmale) { This. UserID = userid; This. uesrname = Uesrname; This. Ismale = Ismale; }//Content description function: When a file descriptor exists for an object, Method 1   Public int describecontents() {return 0; }//Serialization function   Public void Writetoparcel(Parcel out,intFlags) {Out.writeint (userId);    Out.writestring (Uesrname); Out.writeint (Ismale?1:0); Out.writeparcelable (book,0); }//deserialization function   Public Static FinalParcelable.creator<user> Creator =NewParcelable.creator<user> () { PublicUserCreatefromparcel(Parcel in) {return NewUser (in); PublicUser[]NewArray(intSize) {return NewUser[size]; }    };Private User(Parcel in)      {userId = In.readint ();      Uesrname = In.readstring (); Ismale = in.readint () = =1;//Pass the current thread's context class loaderBook = in.readparcelable (Thread.CurrentThread (). Getcontextclassloader ()); }  }}
Comparison of Parcelable and serializable
    • Serializable is a serialized interface in Java, which is simple but expensive to use, and requires a lot of I/O operations in the serialization and deserialization process.
    • Parcelable is a serialization method in Android, and the downside is that it's a bit more cumbersome to use, but it's highly efficient and is the recommended serialization for Android.

Recommended on the Android platform, Parcelable but after serializing the object to the storage device or serializing the object over the network transmission recommendationSerializable

    • Email:[email protected]
    • Good luck!

Android Development Art Exploration (study note) IPC mechanism in--03-android (i)

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.