0

im new to Android development and im struggling with the following issue: So at my app im using Firebase realtime database and im trying to set a Broadcast reveiver that will work when device boot is completed. my goal is that the Broadcast receiver will check if some one is listed at some Park more then 15 mins as the device boot up completed. if the user is listed more then 15mins i want it to be removed or else use post delayed and delete him after time that remains. at the emulator the function that may check/remove the value works just fine and at my physical device its not working.

the function:(as for the start i want the data will be deleted as soon as it found)

    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 debugging from emulator :

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 debugging from physical device :

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 
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
מגן לוי
  • 49
  • 1
  • 7

0 Answers0