Intent 传递intent.putExtra("rsd", rsd);这句不可
intent传递非基本类型, 是要通过pacelable或者serializable序列化的, 反序列化unmarshell之后得到的是新对象, 而非原来对象的引用,
所以rsd.notifyUpload失效,因为这个对象里就没有Observer

解决方案 »

  1.   


    我看了一下Intent的putExtra()方法,操作位将数据放到Bundle的Map(mMap = new HashMa<String,Object>())中.取数据的方法为:
      public Serializable getSerializable(String key) {
            unparcel();
            Object o = mMap.get(key);
            if (o == null) {
                return null;
            }
            try {
                return (Serializable) o;
            } catch (ClassCastException e) {
                typeWarning(key, o, "Serializable", e);
                return null;
            }
        }
    在此基础上我做了个测试,一个Water被观察者,Display观察者,显示作用,还有一个Test类。具体代码如下public class Water extends Observable implements Serializable {
    private static final long serialVersionUID = 1L;
    private int time;  //烧水时间
    public Water() {
    };
    public void sendData(){
    setChanged();
    notifyObservers();
    }
    public int getTime() {
    return time;
    }
    public void setTime(int time) {
    this.time = time;
    sendData();
    }
    }
    Display类:
    public class Display implements Observer {
    public Display(){};
    @Override
    public void update(Observable o, Object arg) {
    // TODO Auto-generated method stub
    if(o instanceof Water){
    Water water = (Water)o;
    System.out.println("当前烧水时间:"+ water.getTime());
    }
    }
    }
    测试类:
    public class TestObserver {

    public static void main(String[] args) {
    Water water = new Water();
    Display display = new Display();
    water.addObserver(display);
    //为了模仿Intent的Bundle的数据存储
    HashMap<String, Object> mMap = new HashMap<String,Object>();
    mMap.put("water", water);
    Water mWater = ((Water)(mMap.get("water")));
    for(int i = 0; i < 10; i ++){
    mWater.setTime(i);
    try {
    Thread.sleep(1000*3);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    运行结果:
    当前烧水时间:0
    当前烧水时间:1
    当前烧水时间:2
    当前烧水时间:3
    当前烧水时间:4
    当前烧水时间:5
    当前烧水时间:6
    当前烧水时间:7
    当前烧水时间:8
    当前烧水时间:9可以显示按照要求来显示,因此即使通过intent.putExtra("rsd", rsd),然后在调用Intent.get()返回的对象引用任为统一个。
      

  2.   

    2楼正解   ntent 传递序列化后的对象接收的不是同一个,引用不一样了