文件下载器:
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;
}
}
演示活动:
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在服务中有新的价值,在活动中的价值没有改变。他们不共享相同的对象引用?