Android : 序列化 Parcelable 简单应用

1.Parcelable 介绍

Parcelable 是 Android 提供的一个序列化接口,用于将数据写入 Parcel,以及从 Parcel 中读取数据。一个类只要实现了这个接口,该类的对象就可以被序列化,主要用于 IPC(进程间通信)、Binder 和 Activity 之间的对象传递。

实现 Parcelable 接口需要实现以下方法:

  1. describeContents():这个方法是内容接口描述,默认返回 0 就可以。
  2. writeToParcel(Parcel dest, int flags):该方法将类的数据写入外部提供的 Parcel 中,即打包须要传递的数据到 Parcel 容器保存,以便从 Parcel 容器获取数据。
  3. Parcelable.Creator:这是一个静态的接口,包含两个方法,用于从 Parcel 中构造实现了 Parcelable 的类的实例。

Parcelable 接口的效率比实现 Serializable 接口高,并且支持用于 Intent 数据传递。需要注意的是,只有实现了 Parcelable 接口的类才能被放入 Parcel 中。

2.简单应用 实现跨进程传递数据

使用了dataBinding 简化开发

// 在 build.gradle 文件中加入
android{

    ...
    defaultConfig{
        ...
        //加入
         dataBinding.enabled true
    }

}

1.主页面 activity_main.xml

Android : 序列化 Parcelable 简单应用_第1张图片




    

    

    

        

        

        

        

        

        

        

        

2.跳转的页面 activity_menu.xml

Android : 序列化 Parcelable 简单应用_第2张图片




    

    

    

        

        

        

        

        

        

        

        

        
    

3.Score.java

package com.example.parcelabledemo;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * 序列化 implements Parcelable
 * 用于跨进程传递数据
 */
public class Score implements Parcelable {
    private float math;
    private float english;
    private float chinese;
    private String grade;

    public Score(float math, float english, float chinese) {
        this.math = math;
        this.english = english;
        this.chinese = chinese;
        if (math >= 90 && english >= 90 && chinese >= 90) {
            this.grade = "A";
        } else if (math >= 80 && english >= 80 && chinese >= 80) {
            this.grade = "B";
        } else {
            this.grade = "C";
        }
    }

    protected Score(Parcel in) {
        math = in.readFloat();
        english = in.readFloat();
        chinese = in.readFloat();
        grade = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeFloat(math);
        dest.writeFloat(english);
        dest.writeFloat(chinese);
        dest.writeString(grade);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator CREATOR = new Creator() {
        @Override
        public Score createFromParcel(Parcel in) {
            return new Score(in);
        }

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

    public float getMath() {
        return math;
    }

    public void setMath(float math) {
        this.math = math;
    }

    public float getEnglish() {
        return english;
    }

    public void setEnglish(float english) {
        this.english = english;
    }

    public float getChinese() {
        return chinese;
    }

    public void setChinese(float chinese) {
        this.chinese = chinese;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}

4.Student.java

package com.example.parcelabledemo;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * 序列化 implements Parcelable
 * 用于跨进程传递数据
 */
public class Student implements Parcelable {
    private String name;
    private int age;
    private Score score;

    public Student(String name, int age, Score score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    protected Student(Parcel in) {
        name = in.readString();
        age = in.readInt();
        score = in.readParcelable(Score.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
        dest.writeParcelable(score, flags);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator CREATOR = new Creator() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Score getScore() {
        return score;
    }

    public void setScore(Score score) {
        this.score = score;
    }
}

5. MyApplication.java

package com.example.parcelabledemo;

import android.app.Application;

public class MyApplication extends Application {
    //整个Activity有效 设置全局属性
    // 用于测试 Application不能跨进程传递数据
    Student student;
}

6.MainActivity.java

package com.example.parcelabledemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.parcelabledemo.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        //保存数据跳转页面事件
        binding.btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float math = Float.parseFloat(binding.etMaths.getText().toString());
                float english = Float.parseFloat(binding.etEnglish.getText().toString());
                float chinese = Float.parseFloat(binding.etChinese.getText().toString());
                Score score = new Score(math, english, chinese);

                String name = binding.etName.getText().toString();
                int age = Integer.parseInt(binding.etAge.getText().toString());
                Student student = new Student(name, age, score);
                //传递数据
//                MyApplication myApplication = (MyApplication) getApplication();
//                myApplication.student = student;

                //意图跳转页面
                Intent intent = new Intent(MainActivity.this, MenuActivity.class);
                //bundle 传递 Parcelable 数据
                Bundle bundle = new Bundle();
                bundle.putParcelable("studentData", student);
                intent.putExtra("data", bundle);
                //跳转
                startActivity(intent);
            }
        });
    }
}

7.MenuActivity.java

package com.example.parcelabledemo;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.parcelabledemo.databinding.ActivityMenuBinding;

public class MenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_menu);
        ActivityMenuBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_menu);
        //获取数据
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("data");
        Student student = bundle.getParcelable("studentData");

//        MyApplication myApplication = (MyApplication) getApplication();
//        Student student = myApplication.student;

        binding.etName.setText(student.getName());
        binding.etAge.setText(String.valueOf(student.getAge()));
        binding.etChinese.setText(String.valueOf(student.getScore().getChinese()));
        binding.etMaths.setText(String.valueOf(student.getScore().getMath()));
        binding.etEnglish.setText(String.valueOf(student.getScore().getEnglish()));
        binding.tvGrade.setText(student.getScore().getGrade());
    }
}

8.AndroidManifest.xml        创建新进程测试数据传递




    
    

        
        
        
            
                

                
            
        
    

你可能感兴趣的:(Android,相关,android,笔记)