自己用到的Android 双服务保活(适配8.0)

最近开发的时候,测试小伙伴经常来找我,“为什么咱家程序放到后台,聊了会qq就得重启了呢?”我脑门一亮,“稍等,一会给你”。然后我就进入了程序流氓(进程保活)之旅。
对于进程保活,其实吧,现在对于MIUI、EMUI等等许多高度定制的系统并没有100%的保活方案,该死还是死掉,但是做了一定的操作,还是可以适当的提高存活的。如下就是我用到的保活方案。

1、启动软件的时候激活本地服务和远程服务

        startService (new Intent (this, MainService.class));
        startService (new Intent (this, RemoteService.class));

2、本地服务代码

/**
 * content:后台运行的服务
 * Actor:韩小呆
 * Time:2018/5/3
 */
public class MainService extends Service {
    MyBinder binder;
    MyConn conn;

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        binder = new MyBinder();
        conn = new MyConn();
    }

    class MyBinder extends IMyAidlInterface.Stub {

        @Override
        public String getServiceName() throws RemoteException {
            return MainService.class.getSimpleName();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.bindService(new Intent(MainService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
        return START_STICKY;
    }

    class MyConn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

            Intent intent = new Intent(MainService.this, RemoteService.class);
            if (Build.VERSION.SDK_INT >= 26) {
                  \\适配8.0机制
                MainService.this.startForegroundService(intent);
            } else {
                MainService.this.startService(intent);
            }
            //绑定远程服务
            MainService.this.bindService(new Intent(MainService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(MainService.this, RemoteService.class);
        \\适配8.0机制
        if (Build.VERSION.SDK_INT >= 26) {
            MainService.this.startForegroundService(intent);
         
        } else {
            MainService.this.startService(intent);
        }
        MainService.this.bindService(new Intent(MainService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
    }
}

3、远程服务代码

/**
 * content:后台运行的服务
 * Actor:韩小呆
 * Time:2018/5/3
 */
public class RemoteService extends Service {
    MyConn conn;
    MyBinder binder;

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        conn = new MyConn();
        binder = new MyBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        this.bindService(new Intent(this, MainService.class), conn, Context.BIND_IMPORTANT);

        return START_STICKY;
    }

    class MyBinder extends IMyAidlInterface.Stub {
        @Override
        public String getServiceName() throws RemoteException {
            return RemoteService.class.getSimpleName();
        }
    }

    class MyConn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

            Intent intent = new Intent(RemoteService.this, MainService.class);
            //开启本地服务
            if (Build.VERSION.SDK_INT >= 26) {
               \\适配8.0机制
                RemoteService.this.startForegroundService(intent);
            } else {
                RemoteService.this.startService(intent);
            }
            //绑定本地服务
            RemoteService.this.bindService(new Intent(RemoteService.this, MainService.class), conn, Context.BIND_IMPORTANT);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(RemoteService.this, MainService.class);
        //开启本地服务
        if (Build.VERSION.SDK_INT >= 26) {
           \\适配8.0机制
            RemoteService.this.startForegroundService(intent);
        } else {
            RemoteService.this.startService(intent);
        }
        //绑定本地服务
        RemoteService.this.bindService(new Intent(RemoteService.this, MainService.class), conn, Context.BIND_IMPORTANT);

    }
}

4、创建AIDL实现远近程服务通信


// Declare any non-default types here with import statements

interface IMyAidlInterface {
    String getServiceName();
}

AIDL文件

5、介绍一些关于进程保活的优质博客
【腾讯Bugly干货分享】Android进程保活招式大全
Android中的进程保活
Android进程保活实践

欢迎关注.jpg

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,219评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,913评论 2 59
  • 导读:Android面试中高频率出现的题都在这了。试题大部分从互联网收集,博主下了一番功夫进行梳理总结,难免有不足...
    Maat红飞阅读 55,852评论 61 844
  • Jianwei's blog 首页 分类 关于 归档 标签 巧用Android多进程,微信,微博等主流App都在用...
    justCode_阅读 5,962评论 1 23
  • 迎着呼啸的北风我找到了你的踪迹你扑打在我的脸上一如那久别相逢 我闭上眼睛闻着你奔波而来的泥土气息你冰冷的肌肤深处却...
    杨书书阅读 294评论 2 6