intent传递基本数据还有对象和集合的总结

一. intent 来传递数据
 (系统提供的方便方法)
 通过intent.putExtra把常用的基本类型给放置进来
 intent.putExtra(name, boolean);
 intent.putExtra(name, long);
 intent.putExtra(name, short);
 intent.putExtra(name, int);

 取数据
 intent.getIntExtra(name, defaultValue);
 intent.getBooleanExtra(name, defaultValue);

 在别的地方获取intent的方法

 首先要得到context对象.context.getIntent();然后调用getXXXExtra(String KEY);

 

 二:android给我们提供了一个方便的对象 Bundle 他类似于map
 Bundle bundle = new Bundle();
        bundle.putBoolean(key, value);
        bundle.putByte(key, value);
        bundle.putCharArray(key, value);

        Intent intent = new Intent();
        intent.putExtras(bundle);

取数据

Bundle bundle =  intent.getExtras();

bundle.getBoolean(String Key);

bundle.getint(String Key);

....

一、二两种方法本质是一样的。因为Intent本身带有一个空的Bundle对象;Bundle存储数据的实现原理是Map的方式

 

如何传递复杂的数据类型(Object List<object>),所传递的对象必须实现Serializable或者Parcelable这两个接口

传递的对象被改变,不会改变原来的值

案例一:传递和接收实现Serializable接口的对象

  1) User.java(implements Serializable)

    2) MainActivity.java

    User user = new User;

    Intent intent = new Intent(this,Second.class);

    intent.putExtra("User",user);

   3)  Second.java

    Intent intent = getIntent( );

    User user = intent.getSerializableExtra("User" );

2、实现Parcelable接口

     1)为什么要实现Parfcelable接口来实现在Intent中传递对象?

      a、在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable类。

      b、Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。

 

     注意:Parcelable不能使用在将数据存储在磁盘上的情况,因为Parcelable不能很好的保存数据的持续性在外界有变化的情况下。因此在这种情况下,建议使用Serializable

      

     2) Android中的新的序列化机制

     在Android系统中,针对内存受限的移动设备,因此对性能要求更高,Android系统采用了新的IPC(进程间通信)机制,要求使用性能更出色的对象传输方式。因此Parcel类被设计出来,其定位就是轻量级的高效的对象序列化和反序列化机制。

     Parcel的序列化和反序列化的读写全是在内存中进行,所以效率比JAVA序列化中使用外部存储器会高很多。

 

Parcel类

     就应用程序而言,在常使用Parcel类的场景就是在Activity间传递数据。在Activity间使用Intent传递数据的时候,可以通过Parcelable机制传递复杂的对象。

     Parcel机制:本质上把它当成一个Serialize就可以了。只是Parcel的对象实在内存中完成的序列化和反序列化,利用的是连续的内存空间,因此更加高效。

    

案例:

    步骤1:自定义实体类,实现Parcelable接口,重写其两个方法。

    步骤2:该实体类必须添加一个常量CREATOR(名字大小写都不能使其他的),该常量必须实现Parcelable的内部接口:Parcelable.Creator,并实现该接口中的两个方法。

    User.java如下:

Java代码  收藏代码
  1. package com.example.intent_object;  
  2.   
  3. import android.os.Parcel;  
  4. import android.os.Parcelable;  
  5.   
  6. public class User implements Parcelable {  
  7.     public String name;  
  8.     public int age;  
  9.   
  10.     // 必须要创建一个名叫CREATOR的常量。  
  11.     public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {  
  12.         @Override  
  13.         public User createFromParcel(Parcel source) {  
  14.             return new User(source);  
  15.         }  
  16.         //重写createFromParcel方法,创建并返回一个获得了数据的user对象  
  17.         @Override  
  18.         public User[] newArray(int size) {  
  19.             return new User[size];  
  20.         }  
  21.     };  
  22.   
  23.     @Override  
  24.     public String toString() {  
  25.         return name + ":" + age;  
  26.     }  
  27.   
  28.     // 无参数构造器方法,供外界创建类的实例时调用  
  29.     public User() {  
  30.     }  
  31.   
  32.     // 带参构造器方法私用化,本构造器仅供类的方法createFromParcel调用  
  33.     private User(Parcel source) {  
  34.         name = source.readString();  
  35.         age = source.readInt();  
  36.     }  
  37.   
  38.     @Override  
  39.     public int describeContents() {  
  40.         return 0;  
  41.     }  
  42.   
  43.     // 将对象中的属性保存至目标对象dest中  
  44.     @Override  
  45.     public void writeToParcel(Parcel dest, int flags) {  
  46.         dest.writeString(name);  
  47.         dest.writeInt(age);  
  48.     }  
  49.   
  50.   //省略getter/setter }  

 

 

    其他代码:

Java代码  收藏代码
  1. Bundle bundle = new Bundle();  
  2.                     bundle.putParcelable("user", user);  
  3.                     Intent intent = new Intent(MainActivity.this,  
  4.                             SecondActivity.class);  
  5.                     intent.putExtras(bundle);  

 

  

Java代码  收藏代码
  1. Intent intent = getIntent();  
  2.         Bundle bun = intent.getExtras();  
  3.         User user = bun.getParcelable("user");  
  4.         System.out.println(user);  

 

三在线程中使用Message传递数据时的方法

  message.arg1 = 放入一个整形数据

  message.what = 整形对象

  message.obj = 放一个对象;

  message.setData =   放入一个Bundle对象

posted @ 2015-04-07 15:34  壮汉请留步  阅读(384)  评论(0编辑  收藏  举报