我的应用在后台耗电过多

My app consume too much battery in background

在我的设置中,我看到我的应用程序在后台的电池设置中被标记为“频繁失败”。这是一个测验应用程序。 我的问题是,是什么让我的应用 运行 在后台运行?

我不使用定位服务。 我使用 Glide、Shared Preferences 等工具,但我不知道当我的应用程序关闭时哪个工具在后台运行...

我的 phone 在 API 29.

编辑:我在 activity 中使用 Handler.postDelayed() 也许它会在后台继续?

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

是的,如果有一个 运行nable 计划在一段时间后 运行,它肯定会 运行 即使你的应用程序在后台(但没有死) .

因此,为了节省资源,请在 activity 的 onPause() 方法中调用 nameOfTheHandler.removeCallbacks(nameOfTheRunnable)

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

有关更多信息,请查看 this 文档。