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

编程入门 行业动态 更新时间:2024-10-21 22:58:02
本文介绍了读取Firebase数据时,永远不会调用onDataChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新来的,目前我正在做一个正在后台运行的应用程序,并检查电话中的进程(如果"com.igg.castleclash"处于进程中)将其设置castleclash值设置为true并将数据保存在Firebase中.一切正常,但是我无法使用addValueEventListener从Firebase读取数据.

I'm new here, currently I'm doing app that is working on background and checking processes in phone, if "com.igg.castleclash" is in processes List it set castleclash value to true and saving data in Firebase. Everything works fine, but I can't read the data from Firebase using addValueEventListener.

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

有什么想法为什么不起作用?

Any ideas why this is not working?

推荐答案

存在多个问题.最重要的是,正如您认为的那样,此处理不是在后台"发生的.它在主线程上运行.如文档所述,这是对服务的常见误解:

There are multiple problems. The most significant is that this processing is not occurring "in the background" as you think. It is running on the main thread. This is a common misunderstanding with Services as described in the documentation:

什么是服务?

关于Service类的大多数困惑实际上都围绕着什么 不是 :

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

  • 服务不是独立的过程. Service对象本身并不意味着它在自己的进程中运行;除非另有 指定,它与作为其一部分的应用程序在同一进程中运行 的.
  • 服务不是线程.这本身并不是在主线程之外进行工作的一种手段(以避免Application Not Responding错误).
  • 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 是用于完成工作的便捷Service子类主线程.

An IntentService is a convenient Service subclass for doing work off the main thread.

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

The while(true) loop in startTimer() runs endlessly. The call to addValueEventListener() in onStartCommend() is never executed because startTimer() never returns.

数据库更改侦听器在主线程上运行.由于调用wait()阻止了主线程,因此onDataChange()回调将无法运行(如果成功添加了侦听器).

Database change listeners run on the main thread. Because the main thread is blocked by the call to wait(), the onDataChange() callback would not be able to run (if the listener were successfully added).

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

Also, to see if your write to the database in startTimer() is failing, add a CompletionListener. The most common reason for failure is permission denied caused by incorrect security rules.

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

更多推荐

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

本文发布于:2023-11-26 20:58:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   数据   Firebase   onDataChange

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!