学习笔记之保活技能之前台服务和双进程守护

这次是前台服务和双进程守护两种保活方法。

一、前台服务部分就简单多了,就是开启一个前台服务,然后在这个前台服务内创建一个内部服务,在做相应Android API 相应的处理。具体代码如下:

ForegroundService前台服务里代码:
public class ForegroundService extends Service {

    private static final int SERVICE_ID = 1;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (Build.VERSION.SDK_INT < 18) {
            //4.3
            startForeground(SERVICE_ID, new Notification());
        } else if (Build.VERSION.SDK_INT < 26) {
            //7.0
            startForeground(SERVICE_ID, new Notification());
            //删除通知栏消息
            startService(new Intent(this, InnerService.class));
        } else {
            //设置channel
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            //越小,通知重要性越低
            NotificationChannel channel = new NotificationChannel("channel", "dd", NotificationManager.IMPORTANCE_MIN);
            if (manager != null) {
                manager.createNotificationChannel(channel);
                Notification notification = new NotificationCompat.Builder(this, "channel").build();

                startForeground(SERVICE_ID, notification);
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // 定时打印日志
        mHandler.postDelayed(r, 100);
    }

    public static class InnerService extends Service {

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

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            startForeground(SERVICE_ID, new Notification());
            stopForeground(true);
            stopSelf();
            return super.onStartCommand(intent, flags, startId);
        }
    }

    Handler mHandler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            //do something
            //每隔1s循环执行run方法
            KLog.e("我是前台服务,我还活着..................");
            mHandler.postDelayed(this, 1000);
        }
    };
}

清单内配置:

<service android:name=".service.ForegroundService"/>
<service android:name=".service.ForegroundService$InnerService"/>

最后在MainActivity中开启前台服务

//前台服务
startService(new Intent(this, ForegroundService.class));

二、双进程守护,就是依靠一个本地服务和远程服务,相互拉活(相互启动)来实现始终有一个服务在运行,就算APP不在前台,也还能够跟后台通讯。实现代码也很简单,具体如下

本地服务LocalService:

public class LocalService extends ForegroundService {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        bindService(new Intent(LocalService.this, RemoteService.class), mConnection, Service.BIND_IMPORTANT);
        return super.onStartCommand(intent, flags, startId);
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //1
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //2
            startService(new Intent(LocalService.this, RemoteService.class));
            bindService(new Intent(LocalService.this, RemoteService.class), mConnection, Service.BIND_IMPORTANT);
        }
    };
}

远程服务RemoteService:

public class RemoteService extends ForegroundService {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        bindService(new Intent(RemoteService.this, LocalService.class), mConnection, Service.BIND_IMPORTANT);
        return super.onStartCommand(intent, flags, startId);
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //1
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //2
            startService(new Intent(RemoteService.this, LocalService.class));
            bindService(new Intent(RemoteService.this, LocalService.class), mConnection, Service.BIND_IMPORTANT);
        }
    };

}

然后我们需要在前台服务ForegroundService中修改下onBind方法中创建一个LocalBinder来返回

@Nullable
    @Override
    public IBinder onBind(Intent intent) {
//        return null; // 前台服务保活时使用
        return new LocalBinder(); // 双进程守护时使用
    }

    private class LocalBinder extends Binder {

    }

后面还需要在清单内注册下这两个服务

<service android:name=".service.LocalService" />
<service
    android:name=".service.RemoteService"
    android:process=":remote" /> //注意标注

最后我们在MainActivity中启用这两个服务就OK了,

//  双进程守护
startService(new Intent(this, LocalService.class));
startService(new Intent(this, RemoteService.class));

写文章的不多,有什么不对的和意见欢迎大家多多指教。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

panyingdao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值