当销毁应用程序时,JobIntentService被销毁

时间:2018-04-21 12:36:35

标签: android kotlin

来自Android开发者JobIntentService在Android O或更高版本上运行时,工作将通过 JobScheduler.enqueue 作为作业发送。在较旧版本的平台上运行时,它将使用 Context.startService

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

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)
        })

2 个答案:

答案 0 :(得分:11)

  

我正在学习JobIntentService,在我的情况下,我有一个每秒运行一次的计时器并显示当前的日期和时间

这不适合用于JobIntentService(或其他任何事情,就此而言)。

  

当我的应用程序被销毁时,JobIntentService也会被销毁

JobIntentService的目的是做一些工作 - 一些磁盘I / O,一些网络I / O等 - 然后就消失了。无限期地执行某些操作 一旦onHandleWork()结束,服务就会消失,您的流程可以在此之后的任何时间点终止,这将停止您的Timer

  

如何在销毁应用时运行它

欢迎您使用前景Service,而不是IntentServiceJobIntentService。如果您的流程已终止(例如,由于内存条件较低),请从START_STICKY返回onStartCommand()以要求Android重新启动您的服务。即使这可能被用户终止,但它是用户的设备,而不是你的设备,因此用户可以做任何用户想要的事情。

答案 1 :(得分:-2)

  

On WAKE_LOCK android系统如何知道如何运行你的Intent service?应该有一些接收器应该接收一些intent-action,然后,它可以运行您的接收器代码,您可以通过接收器代码调用或启动您的服务或意图服务。!!

以下是样本清单

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service
        android:name=".WatchMan"
        android:enabled="true"
        android:exported="true" >
  </service>
</application>

以上清单首先要求许可RECEIVE_BOOT_COMPLETED。然后告诉我BOOT_COMPLETED启动我的接收器MyReceiver

MyReceiver.java

public class MyReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d("BootTest : ", "\nOnBootReceiver - Received a broadcast!");
        Toast.makeText(context, "OnBootReceiver Received a broadcast!!", Toast.LENGTH_LONG).show();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            context.startForegroundService(new Intent(context, WatchMan.class));
        }
        else
        {
            context.startService(new Intent(context, WatchMan.class));
        }
    }
}

依次开始WatchMan Service ...希望它有所帮助..

相关问题