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

Java Android服务应用程序-CPU监视器,java,android,service,cpu,Java,Android,Service,Cpu,我正在构建一个android应用程序来监控你的CPU温度。 我想知道,如果我的主应用程序没有运行,是否有办法使类服务,并防止它被终止。 如果可能,怎么做 当CPU达到临界温度并降低时钟时,服务将启动 另外,是否可以知道服务是否正在运行 提前谢谢 我的建议: 为“android.intent.action.BOOT_COMPLETED”添加广播接收器,并在启动时启动服务 在service.onStartCommand()方法中,返回START\u STICKY以确保在系统终止时重新启动服务 在后台

我正在构建一个android应用程序来监控你的CPU温度。 我想知道,如果我的主应用程序没有运行,是否有办法使类服务,并防止它被终止。 如果可能,怎么做

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

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

提前谢谢

我的建议:

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

  • 在service.onStartCommand()方法中,返回START\u STICKY以确保在系统终止时重新启动服务

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

  • 有关“启动”的更多信息:

    /**
    *从{@link#onStartCommand}返回的常量:如果此服务
    *进程在启动时被终止(从返回后
    *{@link#onStartCommand}),然后将其保持在启动状态,但
    *不要保留此交付意图。稍后,系统将尝试
    *重新创建服务。因为它处于启动状态,所以它将
    *保证在创建新的
    *服务实例;如果没有任何待执行的启动命令
    *传递到服务时,将以空意图调用它
    *对象,因此您必须注意检查此项。
    * 
    *此模式对于将显式启动的事物是有意义的
    *并停止运行任意时间段,如服务
    *播放背景音乐。
    */
    公共静态最终int START_STICKY=1;
    
    “当CPU达到临界温度并降低时钟时,服务必须启动”-您有什么证据证明您可以完成这部分工作?我正在从/sys/htc/CPU\u temp循环读取温度。谢谢!我还有一个问题:当我退出/关闭我的主应用程序时,服务会被关闭吗?我想阻止它。你说的“退出”应用程序是什么意思?正常的“回家”不会扼杀你的进程。但若你们调用退出或终止进程,你们的服务也会失效;这就是我的用户要做的。。我需要防止它被杀死,即使我的主应用程序已关闭。有可能吗?如果你只是指“回家”的情况,那么你可以坚持我的建议,一切都会好起来的。没有。我正在切换到“后退”按钮,这会终止应用程序。我想我会制作一个单独的应用程序,作为服务运行。。。。。
    /**
     * 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;