4

I have a problem in my code on android. I tried to use Firebase Realtime Database to have a cloud based where I can update a data instantly in return it to me instantly but the the problem is it's only execute the code that i want when the screen is on. What i need is I want to execute a line of code everytime a data has been changed while the screen is off, is that possible ? thanks everybody

@Override
    protected void  onStart(){
        super.onStart();
        mref = new Firebase("https://connection-4f6d8.firebaseio.com/condition"+a_id);
        mref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String text = dataSnapshot.getValue(String.class);
                if(text.equals("UNLOCKED")){
                    sendDataonWIfi("0");
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }

@Override
    protected void onStop() {
        super.onStop();
        mref = new Firebase("https://connection-4f6d8.firebaseio.com/condition"+a_id);
        mref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String text = dataSnapshot.getValue(String.class);
                if(text.equals("UNLOCKED")){
                    sendDataonWIfi("0");
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }
Jaypee Tan
  • 101
  • 1
  • 10
  • If these data Are in a ListView you can try to change it in a RecyclerView – Carlo Jul 26 '16 at 12:15
  • nope, its in the textview – Jaypee Tan Jul 26 '16 at 12:16
  • You should add event listener onCreate. You don't need to add it every time in onStart and onStop. – Arpit Ratan Jul 26 '16 at 12:35
  • Try to use a ChildEventListener and write your code in OnChildChanged – Carlo Jul 26 '16 at 15:47
  • In general, you should not expect that any of your app's code will be running when it is not visible to the user (especially when the screen is off). Android is free to kill your app's process when it is not visible in order to free resources for other things that may need to run instead. If you have an active Service, that may reduce the chance of this happening, but it is still not certain. The best way to notify your app that it should react to some data that has changed, use Firebase Cloud Messaging to ping your app and allow Android to launch it to handle the request. – Doug Stevenson Jul 26 '16 at 19:40

3 Answers3

2

I'm guessing your piece of code is not running when the device goes to sleep mode. To resolve this you have to use Wakelocks. When you acquire a wake lock it prevents the device to go back to sleep mode till the time you have released it.

You can use a wakeful broadcase receiver which takes a wake lock and starts a service. When your job is done release the wake lock.

Be careful in using the wakelocks since it will drain the battery faster if you not let the device go back to sleep mode(low power consumption mode). You can find implementation details in the below link :

https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
0

Using OnStop() to run the code you would like to have running while your screen is on should work for this. To better understand the activity lifecycle. https://developer.android.com/reference/android/app/Activity.html

Meshugah
  • 224
  • 1
  • 16
  • so... if i put the code in the onStop method? it will run a code even if the screen is off? – Jaypee Tan Jul 26 '16 at 12:14
  • That should work yes. All though the developer docs on the android site advice against doing something intensive while the phone is asleep, as they mention it might hinder starting a new activity. https://developer.android.com/training/basics/activity-lifecycle/pausing.html – Meshugah Jul 26 '16 at 12:18
0

I realize this is an old question, but I was having the same issue with Firebase ChildEventListener when the Android device went into idle state. I disabled battery optimization for my app and this seems to have solved it - I believe you can do so programmatically for your users like this: Check if battery optimization is enabled or not for an app

policenauts
  • 512
  • 6
  • 18