简体   繁体   中英

Change progress of ProgressBar

I am creating an app where users enter a specific byte value, and I am using a work manager to mimic the uploading progress for them. So, it's basically this:

int size = 50000; // Depends on what kind of file size the user wants to mimic
for(int i = 0; i < size; i++) {
   //Increase progress
   UploadFile.setUploadProgress(i);
}

This code is in work manager. The UploadFile.java is my activity.

While UploadFile.setUploadProgress(int progress) is a static function in my activity which looks like this:

public static void setUploadProgress(int progress) {
   progressBar.setProgress(progress);
}

progressBar being my progress bar which is defined as:

private static ProgressBar progressBar;

But this is static, so it is logical for it to have memory leaks. I know we can use ThreadLocal<>() in AsyncTask to avoid this, but what can we use something inside a activity and with a static function to avoid memory leaks? Or is there a better way to do this?

If this is a duplicate, please point me to the right direction, because I couldn't find anything.

WorkManager is intended for background jobs. As far as I am aware, you can output the end result only of WorkManager and do with it whatever you want inside the code - note that you can extend from CoroutineWorker to handle coroutines or RxWorker to handle RxJava2. If I were you, I would scope the upload progress to the running activity/fragment and dedicate the it to the WorkManager only if the app goes to background. From WorkManager, I am able to spawn Notification which will inform the user about the progress.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM