From c54003c143d59ddf507c4a37e3ef17d36ff6fb0c Mon Sep 17 00:00:00 2001 From: RuralHunter Date: Wed, 5 Jul 2023 12:25:55 +0800 Subject: [PATCH 1/2] fix status worker leak --- .../com/zfdang/zsmth_android/MainActivity.java | 7 +++++-- .../zfdang/zsmth_android/newsmth/SMTHHelper.java | 2 +- .../services/MaintainUserStatusWorker.java | 16 ++++++++-------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/zfdang/zsmth_android/MainActivity.java b/app/src/main/java/com/zfdang/zsmth_android/MainActivity.java index a9c746e2..4c25e757 100644 --- a/app/src/main/java/com/zfdang/zsmth_android/MainActivity.java +++ b/app/src/main/java/com/zfdang/zsmth_android/MainActivity.java @@ -37,6 +37,7 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.work.Data; +import androidx.work.ExistingWorkPolicy; import androidx.work.OneTimeWorkRequest; import androidx.work.WorkManager; import androidx.work.WorkRequest; @@ -177,18 +178,20 @@ public void onBackStackChanged() { } }); + WorkManager.getInstance(getApplicationContext()).cancelAllWorkByTag(MaintainUserStatusWorker.class.getName()); // setup receiver to receive user status update from periodical background service setupUserStatusReceiver(); // schedule the periodical background service Data.Builder inputData = new Data.Builder(); inputData.putBoolean(MaintainUserStatusWorker.REPEAT, true); - WorkRequest userStatusWorkRequest = + OneTimeWorkRequest userStatusWorkRequest = new OneTimeWorkRequest.Builder(MaintainUserStatusWorker.class) .setInitialDelay(SMTHApplication.INTERVAL_TO_CHECK_MESSAGE, TimeUnit.MINUTES) .setInputData(inputData.build()) .build(); - WorkManager.getInstance(getApplicationContext()).enqueue(userStatusWorkRequest); + WorkManager.getInstance(getApplicationContext()) + .enqueueUniqueWork(MaintainUserStatusWorker.WORKER_ID, ExistingWorkPolicy.KEEP,userStatusWorkRequest); // run the background service now updateUserStatusNow(); diff --git a/app/src/main/java/com/zfdang/zsmth_android/newsmth/SMTHHelper.java b/app/src/main/java/com/zfdang/zsmth_android/newsmth/SMTHHelper.java index b53e8b61..a98cc624 100644 --- a/app/src/main/java/com/zfdang/zsmth_android/newsmth/SMTHHelper.java +++ b/app/src/main/java/com/zfdang/zsmth_android/newsmth/SMTHHelper.java @@ -91,7 +91,7 @@ public class SMTHHelper { // singleton private static SMTHHelper instance = null; - public static SMTHHelper getInstance() { + public static synchronized SMTHHelper getInstance() { if (instance == null) { instance = new SMTHHelper(); } diff --git a/app/src/main/java/com/zfdang/zsmth_android/services/MaintainUserStatusWorker.java b/app/src/main/java/com/zfdang/zsmth_android/services/MaintainUserStatusWorker.java index 950ae194..6010d295 100644 --- a/app/src/main/java/com/zfdang/zsmth_android/services/MaintainUserStatusWorker.java +++ b/app/src/main/java/com/zfdang/zsmth_android/services/MaintainUserStatusWorker.java @@ -8,6 +8,7 @@ import androidx.annotation.NonNull; import androidx.work.Data; +import androidx.work.ExistingWorkPolicy; import androidx.work.OneTimeWorkRequest; import androidx.work.WorkManager; import androidx.work.WorkRequest; @@ -28,6 +29,7 @@ import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.functions.Function; +import io.reactivex.observers.DisposableObserver; import io.reactivex.schedulers.Schedulers; public class MaintainUserStatusWorker extends Worker { @@ -35,6 +37,8 @@ public class MaintainUserStatusWorker extends Worker { public static final String REPEAT = "REPEAT"; private static final String TAG = "MUSWorker"; + public static final String WORKER_ID=MaintainUserStatusWorker.class.getName(); + public MaintainUserStatusWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); } @@ -73,7 +77,7 @@ private void doWorkInternal() { final SMTHHelper helper = SMTHHelper.getInstance(); - //Log.d(TAG, "1.0 get current UserStatus from remote"); + Log.d(TAG, "1.0 get current UserStatus from remote"); helper.wService.queryActiveUserStatus().map(new Function() { @Override public UserStatus apply(UserStatus userStatus) throws Exception { @@ -181,11 +185,7 @@ public UserStatus apply(UserStatus userStatus) throws Exception { } return userStatus; } - }).observeOn(Schedulers.io()).subscribeOn(Schedulers.io()).subscribe(new Observer() { - @Override - public void onSubscribe(Disposable d) { - } - + }).subscribe(new DisposableObserver() { @Override public void onNext(UserStatus userStatus) { //Log.d(TAG, "4.0 onNext: " + userStatus.toString()); @@ -253,11 +253,11 @@ public String getNotificationMessage(UserStatus userStatus) { private void enqueueNextWorker() { Data.Builder inputData = new Data.Builder(); inputData.putBoolean(MaintainUserStatusWorker.REPEAT, true); - WorkRequest userStatusWorkRequest = + OneTimeWorkRequest userStatusWorkRequest = new OneTimeWorkRequest.Builder(MaintainUserStatusWorker.class) .setInitialDelay(SMTHApplication.INTERVAL_TO_CHECK_MESSAGE, TimeUnit.MINUTES) .setInputData(inputData.build()) .build(); - WorkManager.getInstance(getApplicationContext()).enqueue(userStatusWorkRequest); + WorkManager.getInstance(getApplicationContext()).enqueueUniqueWork(WORKER_ID, ExistingWorkPolicy.KEEP,userStatusWorkRequest); } } From a9c8292de0e98418e5161f74ff55e4e0ac122923 Mon Sep 17 00:00:00 2001 From: RuralHunter Date: Wed, 5 Jul 2023 14:49:53 +0800 Subject: [PATCH 2/2] comment log --- .../zfdang/zsmth_android/services/MaintainUserStatusWorker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/zfdang/zsmth_android/services/MaintainUserStatusWorker.java b/app/src/main/java/com/zfdang/zsmth_android/services/MaintainUserStatusWorker.java index 6010d295..b11a2947 100644 --- a/app/src/main/java/com/zfdang/zsmth_android/services/MaintainUserStatusWorker.java +++ b/app/src/main/java/com/zfdang/zsmth_android/services/MaintainUserStatusWorker.java @@ -77,7 +77,7 @@ private void doWorkInternal() { final SMTHHelper helper = SMTHHelper.getInstance(); - Log.d(TAG, "1.0 get current UserStatus from remote"); + //Log.d(TAG, "1.0 get current UserStatus from remote"); helper.wService.queryActiveUserStatus().map(new Function() { @Override public UserStatus apply(UserStatus userStatus) throws Exception {