0

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);
  }
}

DownloadService

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();
    }
});

The problem is that when the FileDownloader object passed by parcel to service. and file_size has new value in service, and the value in activity doesn't change. They don't share the same object reference?

Daniel
  • 861
  • 1
  • 8
  • 12
  • Firstly, you should create and use a `set()` method like `setFileSize(int size)` in `FileDownloader` and set `file_size` as `private`. Also, you should name your variables and methods in camelcase... eg: `onUpdate(int downloadedSize)` and `getFileSize`. Underscores should only be used for `final`/`static` variables. – u3l Jul 14 '14 at 04:04
  • Secondly, the reason you can't access it is because you've initialized a `new FileDownloader()` in `DemoActivity`. You're not using the instance that `DownloadService` has. – u3l Jul 14 '14 at 04:07

0 Answers0