gpt4 book ai didi

android - 读取 Firebase 数据,永远不会调用 onDataChange

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:09 25 4
gpt4 key购买 nike

我是新来的,目前我正在做的应用程序正在后台工作并检查手机中的进程,如果 "com.igg.caSTLeclash" 在进程列表中,它将 caSTLeclash 值设置为true 并在 Firebase 中保存数据。一切正常,但我无法使用 addValueEventListener 从 Firebase 读取数据。

**Firebase data img**

public class SensorService extends Service {
boolean castleclash = false;
DatabaseReference databaaseUsers;


public SensorService(Context applicationContext) {
super();
}

public SensorService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);

startTimer();

databaaseUsers.child("user1").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

UsersData user = dataSnapshot.getValue(UsersData.class);
Log.i("user name", user.getName());
}

@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("Failed to read value.", error.toException());
}
});

return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
Intent broadcastIntent = new
Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
sendBroadcast(broadcastIntent);


}
public void startTimer() {
synchronized (this) {
while (true) {

try {
wait(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
databaaseUsers = FirebaseDatabase.getInstance().getReference();
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
final List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();

for (ActivityManager.RunningAppProcessInfo process : runningProcesses) {
if (process.processName.equals("com.igg.castleclash")) {
castleclash = true;
break;
} else {
castleclash = false;

}
}

UsersData user1 = new UsersData(castleclash, "name");
databaaseUsers.child("user1").setValue(user1);

}

}

}



@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

知道为什么这不起作用吗?

最佳答案

存在多个问题。最重要的是,此处理并不如您所想的那样“在后台”发生。它在主线程上运行。这是对服务的常见误解,如 documentation 中所述。 :

What is a Service?

Most confusion about the Service class actually revolves around what it is not:

  • A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
  • A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

IntentService是一个方便的服务子类,用于在主线程之外完成工作。

startTimer() 中的while(true) 循环无休止地运行。 onStartCommend() 中对 addValueEventListener() 的调用永远不会执行,因为 startTimer() 永远不会返回。

数据库更改监听器在主线程上运行。因为主线程被 wait() 调用阻塞,onDataChange() 回调将无法运行(如果成功添加了监听器)。

此外,要查看您在 startTimer() 中写入数据库是否失败,请添加一个 CompletionListener。失败的最常见原因是不正确的安全规则导致权限被拒绝。

UsersData user1 = new UsersData(castleclash, "name");
databaaseUsers.child("user1").setValue(user1, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError == null) {
Log.d(TAG, "onComplete: success");
} else {
Log.e(TAG, "onComplete: failed", databaseError.toException());
}
}
});

关于android - 读取 Firebase 数据,永远不会调用 onDataChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44741830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com