zoukankan      html  css  js  c++  java
  • android之Parcel机制学习

    Parcel源码的分析,可以参考曹文斌的 探索Android中的Parcel机制(上)

    本质上把Parcel当成一个Serialize,不同在于它是在内存中完成的序列化和反序列化,利用的是连续的内存空间,因此会更加高效。

    在Android中,最常见的使用Parcel类的情况是:在Activity间传递数据。在Activity间通过Intent传递数据的时候,可以利用Parcelable机制传递复杂的对象

    通过创建一个工程来学习使用Parcel机制。

    创建Text.java类实现Parcelable接口。

    package com.cb.test;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class Text implements Parcelable {
        private String string = "hello,cb";
    
        public Text() {
            setString("hello,cb");
        }
    
        public Text(Parcel parcel) {
            setString(parcel.readString());
        }
    
        public static final Parcelable.Creator<Text> CREATOR = new Parcelable.Creator<Text>() {
    
            public Text createFromParcel(Parcel source) {
                return new Text(source);
            }
    
            public Text[] newArray(int size) {
                return new Text[size];
            }
        };
    
        public String getString() {
            return string;
        }
    
        public void setString(String string) {
            this.string = string;
        }
    
        public int describeContents() {
            return 0;
        }
    
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(string);
        }
    }

    接下来通过两个Activity之间传递Text对象来了解Parcel机制。

    MainActivity.java

    package com.cb.test;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        private final int SUB_ACTIVITY = 0;
        private Text text = new Text();
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {   //触发触摸事件,传递Text对象,跳转到SubActivity
            if (event.getAction() == MotionEvent.ACTION_UP) {
                Intent intent = new Intent();
                intent.setClass(this, SubActivity.class);   //设置跳转的类为SubActivity
                text.setString("who is cb?");    //设置text对象的string值
                intent.putExtra("MyText", text);    //将text对象放到intent中
                startActivityForResult(intent,SUB_ACTIVITY);  
            }
            return super.onTouchEvent(event);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {  //回调时,调用此函数处理
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == SUB_ACTIVITY){
                if (resultCode == RESULT_OK){
                    if (data.hasExtra("MyText")){
                        text = data.getParcelableExtra("MyText");  //通过getParcelableExtra方法获取text对象
                        ((TextView) findViewById(R.id.text)).setText(text.getString());
                    }
                }
            }
        }
    }

     SubActivity.java

    package com.cb.test;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.widget.TextView;
    
    public class SubActivity extends Activity {
        private Text text;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Intent intent = getIntent();
            if (intent != null){
                if (intent.hasExtra("MyText")){
                    text = intent.getParcelableExtra("MyText");
                    ((TextView) findViewById(R.id.text)).setText(text.getString());
                }
            }
        }
        
        @Override
        public boolean onTouchEvent(MotionEvent event){  //触发触摸事件,重新设置了text对象是string值,发送intent调用MainActivity。
            if (event.getAction() == MotionEvent.ACTION_UP){
                Intent intent = new Intent();
                if (text != null){
                    text.setString("cb is a handsome boy");
                    intent.putExtra("MyText", text);
                }
                setResult(RESULT_OK,intent);
                finish();
            }
            return super.onTouchEvent(event);
        }
    }

    参考:

    Android中的Parcel是什么  解释很给力

    Android开发:什么是Parcel(2) 对处理的数据类型进行分析

    探索Android中的Parcel机制(下)  具体例子实现

  • 相关阅读:
    Linux 修改最大线程数
    Openresty+Nginx+Lua+Nginx_http_upstream_check_module 搭建
    SSDB 性能测试
    面向对象:类的成员
    封装,多态,类的约束,super()深入了解
    面向对象:继承
    面向对象:类的空间问题,类之间关系
    面向对象初识
    软件开发规范
    模块(四)包和logging日志
  • 原文地址:https://www.cnblogs.com/chenbin7/p/2791329.html
Copyright © 2011-2022 走看看