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

最新文章

  1. Cocoa深入学习:NSOperationQueue、NSRunLoop和线程安全 (转)
  2. 解决Ubuntu安装openssh-server依赖问题
  3. 基于eclipse-java的平台上搭建安卓开发环境
  4. 解决VML遭遇IE8和XHTML DOCTYPE时不能运行的问题(转)
  5. ae_将面积小于1500的Feature同附近Feature进行合并
  6. MVC4发布到IIS7报404错误
  7. bzoj 2245: [SDOI2011]工作安排
  8. C++的辅助工具介绍 [转]
  9. 在有跳板机的情况下,SecureCRT自动连接到目标服务器
  10. ASP.NET MVC 4 插件化架构简单实现-实例篇
  11. SQL中约束和触发器的停用与启用
  12. javaSE-基础部分整理
  13. windows使用python调用wget批处理下载数据
  14. 【Java】Java Socket编程(1)基本的术语和概念
  15. openssl AES加密算法API的使用示例
  16. 分析内存使用情况 (JavaScript)
  17. Nginx location配置详细解释
  18. Alpha 冲刺 (9/10)
  19. go语言中的运算符^,&
  20. redis的主从机制 master&slave

热门文章

  1. logstash异常
  2. C#中拼音模糊匹配汉字智能搜索
  3. ModelForm基本使用
  4. HTML: 引号不能忽视
  5. instanceof和getClass的区别
  6. Atman开发实习生的笔试题
  7. csrf原理及flask的处理方法
  8. 不定参数(rest 参数 ...)
  9. HDU 1024 Max Sum Plus Plus (递推)
  10. 网页中<a>标签新窗口和location.href 新窗口打开