我是Android开发的新手,并且遇到以下问题: 因此,在我的应用程序中,我使用Firebase实时数据库,并尝试设置一个广播接收器,该接收器将在设备启动完成后起作用。 我的目标是在设备启动完成后的15分钟内,广播接收器将检查某个公园中是否列出了某人。 如果列出的用户超过15分钟,我希望将其删除,或者使用post post并在剩余时间后将其删除。 在模拟器上,可以检查/删除值的功能运行正常,而在我的物理设备上,该功能不起作用。
功能:(一开始我希望数据一被发现就被删除)
private void checkExistent(final Context context, Intent intent) {
final DatabaseReference userref;
userref =
FirebaseDatabase.getInstance().
getReference("users/"+mAuth.getCurrentUser().
getUid()+"/parks/currentlyAtPark/");
Log.d(TAG, "2");
Query checkexistent = userref;
checkexistent.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Toast.makeText(context, mAuth.getCurrentUser().getUid(), Toast.LENGTH_SHORT).show();
if(snapshot.exists())
{
Toast.makeText(context, "START 3", Toast.LENGTH_SHORT).show();
Log.d(TAG, "3");
final String parkid = snapshot.child("parkid").getValue(String.class);
int chours = snapshot.child("time").child("hours").getValue(int.class);
int days =snapshot.child("time").child("date").getValue(int.class);
int hours = abs(chours- Calendar.getInstance().getTime().getHours());
if(hours>0||days!=Calendar.getInstance().getTime().getDate())
{
//remove
Log.d(TAG, "4");
Toast.makeText(context, "START 4", Toast.LENGTH_SHORT).show();
removeUserFromPark(parkid,userref);
Toast.makeText(context, "data deleted!", Toast.LENGTH_SHORT).show();
}
else
{
if(hours==0)
{
Log.d(TAG, "5");
Toast.makeText(context, "START 5", Toast.LENGTH_SHORT).show();
int cminutes = snapshot.child("time").child("minutes").getValue(int.class);
int minutes = Calendar.getInstance().getTime().getMinutes()-cminutes;
Toast.makeText(context, "Minutes = "+minutes, Toast.LENGTH_SHORT).show();
if(minutes>15)
{
//remove
Log.d(TAG, "6");
Toast.makeText(context, "START 6", Toast.LENGTH_SHORT).show();
removeUserFromPark(parkid,userref);
Toast.makeText(context, "data deleted!", Toast.LENGTH_SHORT).show();
}
else
{
if(minutes==0)
{
Log.d(TAG, "7");
Toast.makeText(context, "START 7", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "data Will be deleted within 15mins!",
Toast.LENGTH_SHORT).show();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
removeUserFromPark(parkid,userref);
Toast.makeText(context, "data deleted!",
Toast.LENGTH_SHORT).show();
}
}, 1000*15*60);
}
else
{
Log.d(TAG, "8");
Toast.makeText(context, "START 8", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "data Will be deleted!",
Toast.LENGTH_SHORT).show();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
removeUserFromPark(parkid,userref);
Toast.makeText(context, "data deleted!",
Toast.LENGTH_SHORT).show();
}
}, 1000);
}
}
}
}
}
else
{
Log.d(TAG, " 0 ");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
从仿真器调试Logcat:
2020-10-04 17:22:05.652 2606-2606/com.example.myloggin01 D/CompatibilityChangeReporter: Compat change
id reported: 147798919; UID 10151; state: ENABLED
2020-10-04 17:22:05.740 2606-2606/com.example.myloggin01 D/BOOT: 3
2020-10-04 17:22:05.746 2606-2606/com.example.myloggin01 D/BOOT: 5
2020-10-04 17:22:05.829 2606-2606/com.example.myloggin01 D/BOOT: 8
2020-10-04 17:22:07.971 2606-2659/com.example.myloggin01 I/FA: App measurement initialized, version:
31000
2020-10-04 17:22:07.972 2606-2659/com.example.myloggin01 I/FA: To enable debug logging run: adb shell
setprop log.tag.FA VERBOSE
2020-10-04 17:22:07.972 2606-2659/com.example.myloggin01 I/FA: To enable faster debug mode event
从物理设备调试Logcat:
2020-10-04 17:04:43.060 6741-6741/com.example.myloggin01 D/BOOT: 1
2020-10-04 17:04:43.067 6741-6741/com.example.myloggin01 D/BOOT: 2
2020-10-04 17:04:43.108 6741-6794/com.example.myloggin01 D/NetworkSecurityConfig: No Network Security
Config specified, using platform default
2020-10-04 17:04:43.144 6741-6786/com.example.myloggin01 I/FA: App measurement initialized, version:
31000
2020-10-04 17:04:43.144 6741-6786/com.example.myloggin01 I/FA: To enable debug logging run: adb shell
setprop log.tag.FA VERBOSE
2020-10-04 17:04:43.145 6741-6786/com.example.myloggin01 I/FA: To enable faster debug mode event