android 后台保活我大该使用了下面几种:
1.双进程 拉起 —6.0以下
2. JobService --6.0以上
3. 1像素保活 — 怎么说呢,感觉是7.0以下。适应度低。(问题多,就没使用了)
4. 无限保活音乐 ---- 效果最好,但是呢耗电。由于我项目特殊性,也采用了。

还使用了前台服务。 所以效果还是可以了,被杀死情况还算少。但是呢,也是很耗电的。
由于我项目特殊,是给专门的人使用,还会给他们配上充电器。所以就不管了。
下面我说下我怎么实现的。 1像素保活,由于有时候监听不到部分系统的,会闪退等
问题就没有使用了。 最后给大家加上,提供参考。

先说下大概的类:
DownloadService :我们工作的类,也就是为了保活这个service.

GuardService; 守护服务
StepService:主服务
这俩个是双进程 拉起

PlayerMusicService: 无限播放音乐,后面为了好控制关闭,就在工作类中使用无限播放音乐
ScheduleService: JobService 保活方式

ServiceAliveUtils:判断工作服务是否还活着的工作类
public class ServiceAliveUtils {
public static boolean isServiceAlice() {
boolean isServiceRunning = false;
ActivityManager manager = (ActivityManager) MyApption.getMyApplication().getSystemService(Context.ACTIVITY_SERVICE);

    if (manager == null) {        return true;    }    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {            if (" com.example.admin.linyuandome.houtaibaohuo.DownloadService".equals(service.service.getClassName())) {            isServiceRunning = true;        }    }    return isServiceRunning;}

}
双进程:
/**

  • 守护进程 双进程通讯

  • @author LiGuangMin

  • @time Created by 2018/8/17 11:27
    */
    public class GuardService extends Service {
    private final static String TAG = GuardService.class.getSimpleName();
    private ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    //Logger.d(TAG, “GuardService:建立链接”);
    boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
    if (!isServiceRunning) {
    Intent i = new Intent(GuardService.this, DownloadService.class);
    startService(i);
    }
    }

     @Override public void onServiceDisconnected(ComponentName componentName) {     // 断开链接     startService(new Intent(GuardService.this, StepService.class));     // 重新绑定     bindService(new Intent(GuardService.this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT); }

    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
    return new KeepAliveConnection.Stub() {
    @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

         } };

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    startForeground(1, new Notification());
    // 绑定建立链接
    bindService(new Intent(this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
    return START_STICKY;
    }

}

/*
主进程
*/
public class StepService extends Service {
private final static String TAG = StepService.class.getSimpleName();
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//Logger.d(TAG, “StepService:建立链接”);
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
if (!isServiceRunning) {
Intent i = new Intent(StepService.this, DownloadService.class);
startService(i);
}
}

    @Override    public void onServiceDisconnected(ComponentName componentName) {        // 断开链接        startService(new Intent(StepService.this, GuardService.class));        // 重新绑定        bindService(new Intent(StepService.this, GuardService.class), mServiceConnection, Context.BIND_IMPORTANT);    }};@Nullable@Overridepublic IBinder onBind(Intent intent) {    return new KeepAliveConnection.Stub() {        @Override        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {        }    };}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {    startForeground(1, new Notification());    // 绑定建立链接    bindService(new Intent(this, GuardService.class), mServiceConnection, Context.BIND_IMPORTANT);    return START_STICKY;}

}

无限播放音乐:
/*
无限播放音乐
*/
public class PlayerMusicService extends Service {
private final static String TAG = PlayerMusicService.class.getSimpleName();
private MediaPlayer mMediaPlayer;

@Nullable@Overridepublic IBinder onBind(Intent intent) {    return null;}@Overridepublic void onCreate() {    super.onCreate();   // Logger.d(TAG, TAG + "---->onCreate,启动服务");    mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.no_notice);    mMediaPlayer.setLooping(true);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {    new Thread(new Runnable() {        @Override        public void run() {            startPlayMusic();        }    }).start();    return START_STICKY;}private void startPlayMusic() {    if (mMediaPlayer != null) {        mMediaPlayer.start();    }}private void stopPlayMusic() {    if (mMediaPlayer != null) {        mMediaPlayer.stop();    }}@Overridepublic void onDestroy() {    super.onDestroy();    stopPlayMusic();    // 重启自己    Intent intent = new Intent(getApplicationContext(), PlayerMusicService.class);    startService(intent);}

}

/*
JobService
*/
@SuppressLint(“NewApi”)
public class ScheduleService extends JobService {
private static final String TAG = ScheduleService.class.getSimpleName();

@Overridepublic boolean onStartJob(JobParameters params) {    boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();    if (!isServiceRunning) {        Intent i = new Intent(this, DownloadService.class);        startService(i);             //   Logger.d(TAG, "ScheduleService启动了DownloadService");    }    jobFinished(params, false);    return false;}@Overridepublic boolean onStopJob(JobParameters params) {    return false;}

}

启动所有保活服务和工作服务
Intent intent = new Intent(mContext, DownloadService.class);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 6.0 以上 jobservice
useJobServiceForKeepAlive();
}
//双守护线程
startService(new Intent(mContext, StepService.class));
startService(new Intent(mContext, GuardService.class));
// 无限播放
startService(new Intent(mContext, PlayerMusicService.class));

关闭就不说了。后面说

更多相关文章

  1. 通过 WIFI 用 adb 调试 Android(安卓)设备
  2. Android实现换肤功能(一)
  3. Unity3D链接Android手机端数据库
  4. android跨进程事件注入(程序模拟用户输入)
  5. android IBinder通信驱动(摘自《android深入浅出多媒体编程》)
  6. 【专访】爱图腾高级架构师 李鹏军:手机音频接口具有无限的应用潜
  7. Android几种Service常驻内存的小思路
  8. Android(安卓)Native进程间通信实例—Socket本地通信服务端进程
  9. Android(安卓)7.0 Service保活总结

随机推荐

  1. android控件相对布局
  2. google map my demo
  3. 判断Android设备是否连接网络
  4. Android中简单的音乐播放器
  5. Android文件递归遍历
  6. android 不使用布局文件,完全由代码控制布
  7. Android 分享微信小程序
  8. Android:Timer定时器
  9. Android ExpandableListView 带有Checkbo
  10. Android应用程序启动过程源代码分析(2)