gpt4 book ai didi

java - Android服务应用——CPU监控

转载 作者:行者123 更新时间:2023-11-30 11:25:44 24 4
gpt4 key购买 nike

我正在构建一个监控 CPU 温度的 Android 应用程序。我想知道是否有办法制作类服务,并防止它被杀死,如果我的主应用程序没有运行的话。如果可能的话,怎么做?

当 CPU 达到临界温度并降低时钟时,该服务将启动。

另外,是否可以知道服务是否正在运行?

提前致谢!

最佳答案

我的建议:

  1. 为“android.intent.action.BOOT_COMPLETED”添加一个广播接收器并在开机时启动您的服务

  2. 在您的 service.onStartCommand() 方法中,返回 START_STICKY 以确保您的服务在被系统终止时重新启动。

  3. 在后台创建一个线程来循环读取 CPU 温度并完成您的工作。

关于“START_STICKY”的更多信息:

/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), then leave it in the started state but
* don't retain this delivered intent. Later the system will try to
* re-create the service. Because it is in the started state, it will
* guarantee to call {@link #onStartCommand} after creating the new
* service instance; if there are not any pending start commands to be
* delivered to the service, it will be called with a null intent
* object, so you must take care to check for this.
*
* <p>This mode makes sense for things that will be explicitly started
* and stopped to run for arbitrary periods of time, such as a service
* performing background music playback.
*/
public static final int START_STICKY = 1;

关于java - Android服务应用——CPU监控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20181648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com