I have created work manager like below and overrides addOnFailureListener and addOnCanceledListener methods and its working now.
public class ChangeStatusScheduler extends Worker {
private String TAG = "ChangeStatusScheduler";
private String status,userId;
public ChangeStatusScheduler(@NonNull Context appContext, @NonNull WorkerParameters params) {
super(appContext, params);
status = params.getInputData().getString(Constants.getInstance().fb_user_status);
userId = params.getInputData().getString(Constants.getInstance().fb_user_id);
}
@NonNull
@Override
public Result doWork() {
Log.e(TAG, "******* doWork");
try {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put(Constants.getInstance().fb_user_status, status);
DatabaseReference statusRef = FirebaseDatabase.getInstance().getReference(Constants.getInstance().fb_table_user)
.child(userId);
statusRef.updateChildren(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.e(TAG, "******* onComplete");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
Log.e(TAG, "******* onFailure ee :: " + e.getMessage());
}
}).addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
Log.e(TAG, "******* onCanceled");
}
});
}catch (Exception ee){
Log.e(TAG, "******* updateStatus ee :: " + ee.getMessage());
ee.printStackTrace();
}
return Result.success();
}
}
and called that work manager like below
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "******* chat onStop backClicked :: " + backClicked);
if (!backClicked) {
try {
Data inputData = new Data.Builder()
.putString(Constants.getInstance().fb_user_status, Constants.getInstance().userStatusOffline)
.putString(Constants.getInstance().fb_user_id, myUserId).build();
final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(ChangeStatusScheduler.class).setInputData(inputData).build();
WorkManager.getInstance().enqueue(workRequest);
} catch (Exception ee) {
Log.e(TAG, "******* updateStatus ee :: " + ee.getMessage());
ee.printStackTrace();
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…