Use file sharing for cross-process communication in Android

Source: Internet
Author: User

I. Serialization of objects using serializable

Importjava.io.Serializable; Public  class User implements Serializable{    Private Static Final LongSerialversionuid =1LPrivateString username;PrivateString password; PublicStringGetUserName() {returnUsername } Public void Setusername(String username) { This. Username = Username; } PublicStringGetPassword() {returnPassword } Public void SetPassword(String password) { This. Password = password; }}

Serialversionuid is a UID that is written to a file when serializing an object, and is intended to prevent the addition or deletion of some fields or the modification of a type, resulting in a different version of the class and a recovery error when the object is being deserialized for recovery.
Serialization Process:

 user user = new  User ();        User.setusername ( "Zhangsan" );        User.setpassword ( "123" );        ObjectOutputStream os = null ; try  {OS = new  objectoutputstream (new  FILEOUTPU            TStream (Getfilesdir () + "Cache.txt" ));        Os.writeobject (user); } catch  (exception  e) {e.printstacktrace ()        ;            }finally  {try  {os.close ();            } catch  (IOException e) {e.printstacktrace (); }        }

Deserialization recovery process:

        isnull;        try {            isnew ObjectInputStream(new FileInputStream(getFilesDir()+"cache.txt"));            is.readObject();            tv.setText(newUser.getUsername());        catch (Exception e) {            e.printStackTrace();        finally{            try {                is.close();            catch (IOException e) {                e.printStackTrace();            }        }

Ii. using parcelable to serialize objects

ImportAndroid.os.Parcel;Importandroid.os.Parcelable; Public  class Student implements parcelable{    PrivateString username;PrivateString password; Public Student(){    } PublicStringGetUserName() {returnUsername } Public void Setusername(String username) { This. Username = Username; } PublicStringGetPassword() {returnPassword } Public void SetPassword(String password) { This. Password = password; }@Override     Public int describecontents() {return 0; }@Override     Public void Writetoparcel(Parcel dest,intFlags) {dest.writestring (username);    dest.writestring (password); } Public Static FinalParcelable.creator<student> Creator =NewCreator<student> () {@Override         PublicStudent[]NewArray(intSize) {return NewStudent[size]; }@Override         PublicStudentCreatefromparcel(Parcel Source) {return NewStudent (source); }    };Private Student(Parcel Source)        {username = source.readstring ();    Password = source.readstring (); }}

The Writetoparcel method is serialized and implemented by the write method of the parcel. The CREATOR is implemented by the parcel Read method, which enables deserialization.

This allows the Parcelable object to be passed through the intent,bundle.

Intent Intent = new Intent ();Intent. SetClass(mainactivity. this, secondactivity. Class);Bundle bundle = new Bundle ();Student Student = new Student ();Student. Setusername("Lisi");Student. SetPassword("456");Bundle. Putparcelable("Student", student);Intent. Putextras(bundle);StartActivity (Intent);

Serializable requires a large number of I/O operations and requires that the serialized object be stored on the device or network for transmission.
Parcelable is a serialized way of Android, which is cumbersome to use, but highly efficient.

Note: This object must be of type parcelable or serializable when there are objects in the Parcelable object.
The serialization method is as follows:

//user为Serializableprivate User user;dest.writeSerializable(user);user = (User) source.readSerializable();//teacher为Parcelableprivate0);//传递当前线程的上下文类加载器teacher = source.readParcelable(Thread.currentThread().getContextClassLoader());

Use file sharing for cross-process communication 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.