android服务保活,android高级面试题pdf

  • Created by xy on 2016/12/2.

*/

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

/**

  • 每10分钟检查一次链接状态,确保service不被杀掉

*/

private static final int KEEP_ALIVE_INTERVAL = BuildConfig.DEBUG ? 1 * 60 * 1000 : 10 * 60 * 1000;

/**

  • ACTION Start

*/

private static final String ACTION_START = “MainService.Action.Start”;

/**

  • ACTION Alarm

*/

private static final String ACTION_ALARM = “MainService.Action.Alarm”;

/**

  • ACTION end start

*/

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);

}

}

你可能感兴趣的:(程序员,面试,android,移动开发)