通过parcel传递时共享一个相同的对象引用

时间:2014-07-14 01:54:52

标签: android object service parcelable parcel

FileDownloader:

public class FileDownloader implements Parcelable {
  int file_size = 0;

  // Ignore other logic code here

  public int get_file_size() {
    return file_size;
  }

  public void start() {
    Intent download_service = new Intent(context, DownloadService.class);
    download_service.putExtra("download_manager", this);
    context.startService(download_service);
  }
}

下载

public class DownloadService extends Service {
    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        FileDownloader download_manager =
                intent.getParcelableExtra("download_manager");

        download_manager.file_size = 2000;
    }
}

DemoActivity:

FileDownloader fd = new FileDownloader();
fd.start(new ProgressUpdateListener () {
    @Override
    public void on_update(int downloaded_size) {
      // the value is still 0
      fd.get_file_size();
    }
});

问题是当FileDownloader对象通过parcel传递给服务时。和file_size在服务中具有新值,并且活动中的值不会更改。他们不共享相同的对象引用?

0 个答案:

没有答案