本文介绍了当应用程序被销毁时,JobIntentService被销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自android开发人员 JobIntentService 在在Android O或更高版本上,该作品将通过 JobScheduler.enqueue 作为工作分派.在较旧版本的平台上运行时,它将使用 Context.startService .

As From android developer JobIntentService When running on Android O or later, the work will be dispatched as a job via JobScheduler.enqueue. When running on older versions of the platform, it will use Context.startService.

在我的情况下,我正在学习 JobIntentService ,在我的情况下,我有一个计时器,该计时器每秒钟运行一次,并显示当前日期和时间,但是当我的应用程序被销毁时 JobIntentService 也将被销毁,当应用程序被销毁后如何运行

In my case i am learning JobIntentService and in my case i have a timer that run every one second and display the current date and time but when my app get destroyed the JobIntentService also get destroyed, How can i run it when app is destroyed

JobIntentService

class OreoService : JobIntentService() {

    private val handler = Handler()

    companion object {
        private const val JOB_ID = 123

        fun enqueueWork(cxt: Context, intent: Intent){
            enqueueWork(cxt,OreoService::class.java,JOB_ID,intent)
        }
    }

    override fun onHandleWork(intent: Intent) {

        toast(intent.getStringExtra("val"))

        Timer().scheduleAtFixedRate(object : TimerTask() {
            override fun run() {
                println(Date().toString())
            }

        }, Date(),1000)

    }

    override fun onDestroy() {
        super.onDestroy()
        toast("Service Destroyed")
    }

   private fun toast(msg: String){

       handler.post({
           Toast.makeText(applicationContext,msg,Toast.LENGTH_LONG).show()
       })
   }
}

清单

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
....... >
<service android:name=".service.OreoService"
            android:permission="android.permission.BIND_JOB_SERVICE"/>
</application>

MainActivity(按下按钮即可启动服务)

startServiceBtn.setOnClickListener({
            val intent = Intent()
            intent.putExtra("val","testing service")
            OreoService.enqueueWork(this,intent)
        })

推荐答案

这不是 JobIntentService 的合适用法(或者与此有关的几乎所有其他东西).

That is not a suitable use for JobIntentService (or pretty much anything else, for that matter).

JobIntentService 的重点是要做一些工作—一些磁盘I/O,一些网络I/O等.然后走开.不是无限期地做某事,不是适合开始异步工作,并且您正在尝试同时做这两项.一旦 onHandleWork()结束,该服务就会消失,并且您的进程可以在此后的任何时间终止,这将停止您的 Timer .

The point of JobIntentService is to do a little bit of work — some disk I/O, some network I/O, etc. — and then go away. It is not for doing something indefinitely, and it is not suitable for starting asynchronous work, and you are trying to do both. Once onHandleWork() ends, the service goes away, and your process can be terminated at any point in time after that, which will stop your Timer.

欢迎您使用前台 Service ,而不是 IntentService JobIntentService .从 onStartCommand()返回 START_STICKY ,以要求Android如果您的进程终止(例如由于内存不足)而重启服务.即使这可能由用户终止,但它是用户的设备,而不是您的设备,因此用户可以执行用户想要执行的任何操作.

You are welcome to use a foreground Service, not an IntentService or JobIntentService. Return START_STICKY from onStartCommand() to ask Android to restart your service if your process is terminated (e.g., due to low memory conditions). Even that may be terminated by the user, but it is the user's device, not yours, and so the user can do whatever the user wants.

这篇关于当应用程序被销毁时,JobIntentService被销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 21:37