Android服务应用程序 - CPU监视器

时间:2013-11-24 22:04:14

标签: java android service cpu

我正在构建一个监视CPU温度的android应用程序。 我想知道是否有一种方法可以提供类服务,并防止它被杀死,如果我的主应用程序没有运行就会发生事件。 如果有可能,怎么样?

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

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

提前致谢!

1 个答案:

答案 0 :(得分:1)

我的建议:

  1. 为“android.intent.action.BOOT_COMPLETED”添加广播接收器并在启动时启动服务

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

  3. 在后台创建一个线程来循环并读取CPU温度并开始工作。

  4. 有关“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;