zoukankan      html  css  js  c++  java
  • 定义一个JobService,开启本地服务和远程服务

    @SuppressWarnings(value = ["unchecked", "deprecation"])
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    class JobHandlerService : JobService() {

    private var mJobScheduler: JobScheduler? = null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    var startId = startId
    startService(this)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    mJobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
    val builder = JobInfo.Builder(startId++,
    ComponentName(packageName, JobHandlerService::class.java.name))
    if (Build.VERSION.SDK_INT >= 24) {
    builder.setMinimumLatency(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS) //执行的最小延迟时间
    builder.setOverrideDeadline(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS) //执行的最长延时时间
    builder.setMinimumLatency(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS)
    builder.setBackoffCriteria(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS, JobInfo.BACKOFF_POLICY_LINEAR)//线性重试方案
    } else {
    builder.setPeriodic(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS)
    }
    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
    builder.setRequiresCharging(true) // 当插入充电器,执行该任务
    mJobScheduler?.schedule(builder.build(http://www.amjmh.com/v/))
    }
    return Service.START_STICKY
    }

    private fun startService(context: Context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    if (KeepLive.foregroundNotification != null) {
    val intent = Intent(applicationContext, NotificationClickReceiver::class.java)
    intent.action = NotificationClickReceiver.CLICK_NOTIFICATION
    val notification = NotificationUtils.createNotification(this, KeepLive.foregroundNotification!!.getTitle(), KeepLive.foregroundNotification!!.getDescription(), KeepLive.foregroundNotification!!.getIconRes(), intent)
    startForeground(13691, notification)
    }
    }
    //启动本地服务
    val localIntent = Intent(context, LocalService::class.java)
    //启动守护进程
    val guardIntent = Intent(context, RemoteService::class.java)
    startService(localIntent)
    startService(guardIntent)
    }

    override fun onStartJob(jobParameters: JobParameters): Boolean {
    if (!isServiceRunning(applicationContext, "com.xiyang51.keeplive.service.LocalService") || !isServiceRunning(applicationContext, "$packageName:remote")) {
    startService(this)
    }
    return false
    }

    override fun onStopJob(jobParameters: JobParameters): Boolean {
    if (!isServiceRunning(applicationContext, "com.xiyang51.keeplive.service.LocalService") || !isServiceRunning(applicationContext, "$packageName:remote")) {
    startService(this)
    }
    return false
    }

    private fun isServiceRunning(ctx: Context, className: String): Boolean {
    var isRunning = false
    val activityManager = ctx
    .getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
    val servicesList = activityManager
    .getRunningServices(Integer.MAX_VALUE)
    val l = servicesList.iterator()
    while (l.hasNext()) {
    val si = l.next()
    if (className == si.service.className) {
    isRunning = true
    }
    }
    return isRunning
    }
    }

  • 相关阅读:
    ecshop 整合 kindedotor
    css 一些小笔记
    linux 使用 随记录
    GIPZ 压缩
    js 代码 随记
    map和list循环遍历
    向数据库批量处理事件
    链表和数组的优劣比较
    内存对齐 和 sizeof小结
    C++的默认构造函数与构造函数
  • 原文地址:https://www.cnblogs.com/ly570/p/11291188.html
Copyright © 2011-2022 走看看