*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class JobSchedulerService extends JobService{
@Override
public boolean onStartJob(JobParameters params) {
startMainService();
jobFinished(params, false);
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
startMainService();
return false;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
startMainService();
}
public void startMainService(){
startService(MainService.getIntentAlarm(this));
}
}
注册JobSchedulerService
android:name=“com.aoaoyi.service.JobSchedulerService” android:permission=“android.permission.BIND_JOB_SERVICE” android:enabled=“true” android:exported=“true” android:process=":push"/> 权限 MainService package com.aoaoyi.service; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.IBinder; public class MainService extends Service { /** */ private static final int KEEP_ALIVE_INTERVAL = BuildConfig.DEBUG ? 1 * 60 * 1000 : 10 * 60 * 1000; /** */ private static final String ACTION_START = “MainService.Action.Start”; /** */ private static final String ACTION_ALARM = “MainService.Action.Alarm”; /** */ private static final String ACTION_END_START = “MainService.Action.EndStart”; private boolean mIsAddAliveAlarm = false; public MainService() { } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { if (!mIsAddAliveAlarm){ addAliveAlarm(); mIsAddAliveAlarm = true; } super.onCreate(); } @Override public int onStartCommand(Intent pIntent, int pFlags, int pStartId) { if (null != pIntent){ switch (pIntent.getAction()){ case ACTION_START: break; case ACTION_ALARM: break; case ACTION_END_START: break; } } if (!mIsAddAliveAlarm){ addAliveAlarm(); mIsAddAliveAlarm = true; } return START_STICKY; } @Override public void onTaskRemoved(Intent pIntent) { onEnd(); } @Override public void onDestroy() { onEnd(); super.onDestroy(); } private void onEnd() { startService(getIntentEndStart(getApplicationContext())); } /** */ private void addAliveAlarm() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { JobInfo.Builder _Builder = new JobInfo.Builder(0, new ComponentName(getAp 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》 【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享 plication(), JobSchedulerService.class)); _Builder.setPeriodic(KEEP_ALIVE_INTERVAL); _Builder.setPersisted(true); JobScheduler _JobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); _JobScheduler.schedule(_Builder.build()); }else { PendingIntent _PendingIntent = PendingIntent.getService(this, 0, getIntentAlarm(this), PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager _AlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); _AlarmManager.cancel(_PendingIntent); _AlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + KEEP_ALIVE_INTERVAL, KEEP_ALIVE_INTERVAL, _PendingIntent); } }