I need to upload the data to the server in a different thread (not on the main thread). I have tried WorkManager to do this job like below. But WorkManager is not getting triggered every time I background the app. How can I send the data to the server in a different thread while the app goes to the background every time?
@Override
public void onCreate() {
...
mRequest = new OneTimeWorkRequest.Builder(UploaddWorker.class).setConstraints(
new Constraints.Builder().setRequiredNetworkType(
NetworkType.CONNECTED).build()).build();
...
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onAppBackgrounded() {
//App in background
Log.e(TAG, "onAppBackgrounded");
mWorkManager.enqueue(mRequest);
}