Android六大進程間通信方式總結之一:基本知識


因為不同進程都是享有獨立資源的,所以全局變量這些都是無效的,必須有其他的進程間通信方式。

 

一、基本知識

 1:怎樣使用多進程

Android正常使用的多進程的辦法只有一種,就是在Service或Activity的屬性里面設置remote屬性

這樣,我們打開Main2Activty的時候,該活動或服務就會以一個新的進程來創建

 

 

2:查看進程信息

通過adb shell連接上我們的手機,輸入ps命令,查看所有的進程

可以使用ps|grep命令 + 包名 直接查看我們的app的進程信息   這里的grep是查詢文本的命令

 

可以看到,有兩個不同的進程。第一個是我們的MainActivity,沒有指定屬性,默認進程名是包名。第二是Main2Activity,默認是包名+:remote

 

 

3:對象的序列化

3.1:什么是序列化?

簡單點來說,序列化就是把我們包含了數據的對象,轉化成一堆數據,保存在文件中。

 

3.2:序列化的作用?

(1).永久的保存對象數據(將對象數據保存在文件當中,或者是磁盤中

2).通過序列化操作將對象數據在網絡上進行傳輸(由於網絡傳輸是以字節流的方式對數據進行傳輸的.因此序列化的目的是將對象數據轉換成字節流的形式)

3).將對象數據在進程之間進行傳遞(Activity之間傳遞對象數據時,需要在當前的Activity中對對象數據進行序列化操作.在另一個Activity中需要進行反序列化操作講數據取出)

4).Java平台允許我們在內存中創建可復用的Java對象,但一般情況下,只有當JVM處於運行時,這些對象才可能存在,即,這些對象的生命周期不會比JVM的生命周期更長(即每個對象都在JVM中)但在現實應用中,就可能要停止JVM運行,但有要保存某些指定的對象,並在將來重新讀取被保存的對象。這是Java對象序列化就能夠實現該功能。(可選擇入數據庫、或文件的形式保存)

5).序列化對象的時候只是針對變量進行序列化,不針對方法進行序列化.

6).在Intent之間,基本的數據類型直接進行相關傳遞即可,但是一旦數據類型比較復雜的時候,就需要進行序列化操作了.

 可以簡單的理解為,轉化為數據流之后,方便計算機直接傳數據或保存數據。

 

3.3:怎么序列化

自定義的類必須實現 Serializable 或者 Parcelable 接口

 

3.3.1:Serializable 

這是Java提供的一個序列化的接口,非常簡單,只需自行聲明一個版本ID即可,其他的都已經封裝好了。

 

//自定義類

public class SerialUser implements Serializable {

    //版本號
    private static final long serialVersionUID = 2016L;

    //成員
    String name;
    int age;

    //構造函數
    public SerialUser(String name,int age){

        this.name = name;
        this.age = age;

    }
}

 

//序列化過程 MainActivity

public class MainActivity extends AppCompatActivity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);

        //使用序列化的類
        //序列化過程
        SerialUser user = new SerialUser("小明",20);
        try {

            File file = new File(Environment.getExternalStorageDirectory()
                     + "/my.txt");



            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
            out.writeObject(user);
            out.close();


        } catch (IOException e) {
            Log.d("TAG",e.toString());
        }


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);

            }
        });

    }
}

 

//反序列化過程 Main2Activity

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Log.d("TAG","new process");

        //反序列化的過程
        try {

            File file = new File(Environment.getExternalStorageDirectory()
                    + "/my.txt");

            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            SerialUser user = (SerialUser)in.readObject();
            in.close();
            Log.d("TAG","ok");
            Log.d("TAG",user.name+user.age);


        } catch (IOException e) {
            Log.d("TAG",e.toString());
        } catch (ClassNotFoundException e) {
            Log.d("TAG",e.toString());
        }


    }
}

 

效果圖

 

順便看看中間文件my.txt里面到底存了什么數據

 

 

3.3.2:Parcelable

Parcelable不像Serialable那樣,可以直接指定中間文件。所以反序列化時不能夠通過打開文件讀取的方式來進行。

我們要實現完整Parcelable接口就行了。Parcelable一般是通過Intent使用的,放進Intent里面來傳遞數據。

public class ParcelUser implements Parcelable {

    //成員
    String name;
    String sex;
    int age;

    //構造
    public  ParcelUser(String name,String sex,int age){

        this.name = name;

        this.sex = sex;

        this.age = age;

    }


    //一般都是返回0,不用管
    @Override
    public int describeContents() {
        return 0;
    }

    //必須實現的
    //其實只是針對成員變量保存一下就好
    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(name);
        dest.writeString(sex);
        dest.writeInt(age);

    }

    //反序列化,這個地方系統沒有提供來實現,自己要補上
    //該實現的地方都實現就對了
    public static final Parcelable.Creator<ParcelUser> CREATOR = new Creator<ParcelUser>() {
        @Override
        public ParcelUser createFromParcel(Parcel source) {
            return new ParcelUser(source







            );
        }

        @Override
        public ParcelUser[] newArray(int size) {
            return new ParcelUser[size];
        }
    };

    //反序列化的構造函數
    private ParcelUser(Parcel in){

        this.name = in.readString();
        this.sex = in.readString();
        this.age = in.readInt();

    }

}

 

MainActivity:

public class MainActivity extends AppCompatActivity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);





        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this,Main2Activity.class);

                ParcelUser user = new ParcelUser("小紅","女",16);

                intent.putExtra("user",user);

                startActivity(intent);

            }
        });

    }
}

 

Main2Activity:

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Log.d("TAG", "new process");

        //反序列化的過程
        Intent intent = getIntent();

        ParcelUser user = intent.getParcelableExtra("user");

        Log.d("TAG",user.name);
        Log.d("TAG",user.sex);
        Log.d("TAG",user.age+"");


    }

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM