firebase oncomplete从未在android 10中调用过

laik7k3q  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(346)

我想从聊天活动onresume和onpause方法更改firebase实时数据库中的状态(联机/脱机)。
我的代码如下:

@Override
protected void onResume() {
    super.onResume();
    updateStatus(Constants.getInstance().userStatusOnline);
}

@Override
protected void onPause() {
    super.onPause();
    updateStatus(Constants.getInstance().userStatusOffline);
}

public void updateStatus(String status) {
    Log.e(TAG, "## updateStatus status : " + status);
    if (myUserId != null) {
        databaseReference = FirebaseDatabase.getInstance().getReference(Constants.getInstance().fb_table_user).child(myUserId);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("status", status);

        databaseReference.updateChildren(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Log.e(TAG, "*******updateStatus onComplete isSuccessful :: " + task.isSuccessful());

            }
        });
    }
}

它在其他android版本中运行良好,但在android10中不会调用oncomplete方法。我也尝试过更新作业调度器和工作管理器中的孩子,但没有成功。
我到处都看到firebasedatabase操作是在后台线程上异步执行的
事先谢谢你的帮助。

5us2dqdw

5us2dqdw1#

我创建了如下所示的工作管理器,覆盖了addonfailurelistener和addoncanceledlistener方法及其现在的工作状态。

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

给下面的工作经理打电话

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

相关问题