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

简介: 最近开发的时候,测试小伙伴经常来找我,“为什么咱家程序放到后台,聊了会qq就得重启了呢?”我脑门一亮,“稍等,一会给你”。然后我就进入了程序流氓(进程保活)之旅。

最近开发的时候,测试小伙伴经常来找我,“为什么咱家程序放到后台,聊了会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();
}

img_39e9215ea9d3259f697457c7f6e0b244.png
AIDL文件
相关文章
|
19天前
|
Java API 调度
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
41 0
|
20天前
|
Android开发
Android 11 添加Service服务SELinux问题
Android 11 添加Service服务SELinux问题
42 1
|
5月前
|
Web App开发 移动开发 小程序
"项目中mpaas升级到10.2.3 适配Android 14之后 app中的H5以及小程序都访问不了,
"项目中mpaas升级到10.2.3 适配Android 14之后 app中的H5以及小程序都访问不了,显示“网络不给力,请稍后再试”,预发内网版本不能使用,线上版本可以正常使用,这个是什么原因啊,是某些参数没有配置吗,还是说是一些参数改错了?
64 2
|
6月前
|
开发工具 数据库 Android开发
0001Java安卓程序设计-基于Android多餐厅点餐桌号后厨前台服务设计与开发2
0001Java安卓程序设计-基于Android多餐厅点餐桌号后厨前台服务设计与开发
30 0
|
5月前
|
XML Java Android开发
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
182 0
|
12天前
|
安全 网络安全 Android开发
云端防御策略:融合云服务与网络安全的未来构建高效的Android应用:从内存优化到电池寿命
【4月更文挑战第30天】 随着企业加速向云计算环境转移,数据和服务的云端托管成为常态。本文探讨了在动态且复杂的云服务场景下,如何构建和实施有效的网络安全措施来保障信息资产的安全。我们将分析云计算中存在的安全挑战,并展示通过多层次、多维度的安全框架来提升整体防护能力的方法。重点关注包括数据加密、身份认证、访问控制以及威胁检测与响应等关键技术的实践应用,旨在为读者提供一种结合最新技术进展的网络安全策略视角。 【4月更文挑战第30天】 在竞争激烈的移动市场中,Android应用的性能和资源管理已成为区分优秀与平庸的关键因素。本文深入探讨了提升Android应用效率的多个方面,包括内存优化策略、电池
|
20天前
|
Android开发
Android RIL 动态切换 4G 模块适配
Android RIL 动态切换 4G 模块适配
18 0
|
29天前
|
编解码 人工智能 测试技术
安卓适配性策略:确保应用在不同设备上的兼容性
【4月更文挑战第13天】本文探讨了提升安卓应用兼容性的策略,包括理解平台碎片化、设计响应式UI(使用dp单位,考虑横竖屏)、利用Android SDK的兼容工具(支持库、资源限定符)、编写兼容性代码(运行时权限、设备特性检查)以及优化性能以适应低端设备。适配性是安卓开发的关键,通过这些方法可确保应用在多样化设备上提供一致体验。未来,自动化测试和AI将助力应对设备碎片化挑战。
|
1月前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
29 1
|
1月前
|
Android开发
Android Uri转File方法(适配android 10以上版本及android 10以下版本)
Android Uri转File方法(适配android 10以上版本及android 10以下版本)
32 0