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

  • 相关阅读:
    spring boot使用自定义注解+AOP实现对Controller层指定方法的日志记录
    spring事务管理中,注解方式和xml配置方式优先级谁高?
    synchronized修饰类中不同方法,调用的时候方法互斥吗
    java(spring boot)实现二维码生成(可以插入中间log和底部文字)
    java借助Robot给微信好友自动发消息(可发送表情包)
    js中Map类型的使用
    【转】Intellij笔记
    Tomcat6.0webappsevopWEB-INFclasses (系统找不到指定的路径)
    多线程多进程之其他
    文件操作
  • 原文地址:https://www.cnblogs.com/ly570/p/11291188.html
Copyright © 2011-2022 走看看