• 定义一个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
    }
    }

  • 相关阅读:
    C# 创建Excel并写入内容
    c#中使用excel
    C#中EXCEL表格的内容进度条实现
    WinForm c#操作Excel
    如何使用 Visual C# .NET 处理 Excel 事件
    C#与Excel的交互示例
    c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
    C#在excel中添加超链接
    ASP.NET学习笔记(3)
    ASP.NET学习笔记(4)
  • 原文地址:https://www.cnblogs.com/ly570/p/11291188.html
Copyright © 2020-2023  润新知