Search code examples
androidbattery

My app consume too much battery in background


In my settings, I saw that my app is marked as "Frequent failures" in battery settings in the background. It is a quiz app. My question is what makes my app running in the background?

I don't use location services. I use tools like Glide, Shared Preferences but I don't know what tool runs in the background when my app is closed...

My phone is on API 29.

EDIT: I use in an activity a Handler.postDelayed() maybe it continues in the background?


Solution

  • I use in an activity a Handler.postdelayed() maybe it continues in background?

    Yes, if there's a runnable which is scheduled to run after some time, it will definitely run even if your app is in the background (but not dead).

    Hence, to save resources, call nameOfTheHandler.removeCallbacks(nameOfTheRunnable) inside the activity's onPause() method.

    @Override
    protected void onPause() {
        super.onPause();
        nameOfTheHandler.removeCallbacks(nameOfTheRunnable)
    }
    

    For more info checkout this documentation.