连接充电器后运行代码背景 [英] Run code background when charger gets connected

查看:46
本文介绍了连接充电器后运行代码背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次充电器连接到设备且应用程序关闭时,我都在尝试运行一些代码.我发现的唯一事情是安排了一个只能在连接充电器时才能运行的作业,但是问题是该代码仅可以运行一次(除非我重新安排了它的工作时间),并且它只能在集合中运行.时代.我希望代码能在每次连接充电器时立即运行,就像广播收听器一样,但要在后台运行.

I'm trying to run some code every time the charger gets connected to the device and the app is closed. The only thing I've found is to schedule a job that only can run when the charger is connected, but the problem with that is that the code will only run once (unless I reschedule it ofc) and it will only run at the set times. I want the code to run immediately and every time I connect the charger, like a broadcastlistener, but in the background.

这可能吗?或者您还有其他建议吗?

Is this possible or do you have some other suggestions?

提前谢谢!:)

如果您正在运行API级别26+,似乎您再也听不到这类广播了.参考

It seems like you can't listen to these kinds of broadcasts anymore if you're running API level 26+ ref

推荐答案

您可以使用 JobScheduler ,然后使用

You could use a JobScheduler, and use the setRequiresCharging(boolean requiresCharging) method on the JobInfo.Builder you create.

这篇关于连接充电器后运行代码背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆