第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Android應(yīng)用進(jìn)程防殺指南2-雙進(jìn)程守護(hù)

標(biāo)簽:
Android

 在上一篇博客中,我们谈到了Android进程防杀的常用套路,各位童鞋可以点击查看我的博客Android应用进程防杀指南1-常用套路,随着越来越多的用户手机安装某卫士,某管家,常用的进程防杀套路并不能够很好的保证我们的APP进程常驻,因此本篇博客我给大家带来抗某卫士,某管家的双进程守护解决方案。双进程守护,其实原理非常简单,网上也有很多博文讲述,今天我给大家用一个完整的案例来讲述。

假设我们的APP中开启了两个Service,分别是A和B,那么: 
如果A守护B,则B挂掉的同时,A就应该把B启动起来,反之亦然,也就是说A和B应该是互相守护,无论谁被杀掉,对方就把它拉起来。

既然提到了两个Service,那么这两个Service就不能让它们同处在一个进程中,否则就会被一次性双杀。这里我们很容易想到IPC技术,在Android中通常我们可以使用AIDL来实现IPC操作。废话不多说,直接撸码。 

先来看下项目整体结构:

这里写图片描述

这里,我们定义两个Service,分别是LocalCastielService和RemoteCastielService,其中的RemoteCastielService我们通过属性配置android:process=”:com.castiel.remote” ,让它成为远端进程。 
LocalCastielService.java

/**
 * @ClassName: LocalCastielService 
 * @Description: 本地服务
 * @author 猴子搬来的救兵 http://blog.csdn.net/mynameishuangshuai
 * @version
 */
public class LocalCastielService extends Service {

    MyBinder myBinder;
    private PendingIntent pintent;
    MyServiceConnection myServiceConnection;

    @Override
    public void onCreate() {
        super.onCreate();
        if (myBinder == null) {
            myBinder = new MyBinder();
        }
        myServiceConnection = new MyServiceConnection();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.bindService(new Intent(this, RemoteCastielService.class), myServiceConnection, Context.BIND_IMPORTANT);
        Notification notification = new Notification(R.drawable.ic_launcher, "猴子服务启动中", System.currentTimeMillis());
        pintent = PendingIntent.getService(this, 0, intent, 0);
        notification.setLatestEventInfo(this, "猴子服务", "防止被杀掉!", pintent);

        // 设置service为前台进程,避免手机休眠时系统自动杀掉该服务
        startForeground(startId, notification);
        return START_STICKY;
    }

    class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            Log.i("castiel", "远程服务连接成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            // 连接出现了异常断开了,RemoteService被杀掉了
            Toast.makeText(LocalCastielService.this, "远程服务Remote被干掉", Toast.LENGTH_LONG).show();
            // 启动RemoteCastielService
            LocalCastielService.this.startService(new Intent(LocalCastielService.this, RemoteCastielService.class));
            LocalCastielService.this.bindService(new Intent(LocalCastielService.this, RemoteCastielService.class),
                    myServiceConnection, Context.BIND_IMPORTANT);
        }

    }

    class MyBinder extends CastielProgressConnection.Stub {

        @Override
        public String getProName() throws RemoteException {
            return "Local猴子搬来的救兵 http://blog.csdn.net/mynameishuangshuai";
        }

    }

    @Override
    public IBinder onBind(Intent arg0) {
        return myBinder;
    }

}

RemoteCastielService.java

/**
 * 
 * @ClassName: RemoteCastielService 
 * @Description: 远程服务
 * @author 猴子搬来的救兵 http://blog.csdn.net/mynameishuangshuai
 */
public class RemoteCastielService extends Service {
    MyBinder myBinder;
    private PendingIntent pintent;
    MyServiceConnection myServiceConnection;

    @Override
    public void onCreate() {
        super.onCreate();
        if (myBinder == null) {
            myBinder = new MyBinder();
        }
        myServiceConnection = new MyServiceConnection();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.bindService(new Intent(this,LocalCastielService.class), myServiceConnection, Context.BIND_IMPORTANT);
        Notification notification = new Notification(R.drawable.ic_launcher,
                "猴子服务启动中",
                System.currentTimeMillis());
        pintent=PendingIntent.getService(this, 0, intent, 0);
        notification.setLatestEventInfo(this, "猴子服务",
                "防止被杀掉!", pintent);

        //设置service为前台进程,避免手机休眠时系统自动杀掉该服务
        startForeground(startId, notification);
        return START_STICKY;
    }

    class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            Log.i("castiel", "本地服务连接成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            // 连接出现了异常断开了,LocalCastielService被杀死了
            Toast.makeText(RemoteCastielService.this, "本地服务Local被干掉", Toast.LENGTH_LONG).show();
            // 启动LocalCastielService
            RemoteCastielService.this.startService(new Intent(RemoteCastielService.this,LocalCastielService.class));
            RemoteCastielService.this.bindService(new Intent(RemoteCastielService.this,LocalCastielService.class), myServiceConnection, Context.BIND_IMPORTANT);
        }

    }

    class MyBinder extends CastielProgressConnection.Stub {

        @Override
        public String getProName() throws RemoteException {
            return "Remote猴子搬来的救兵 http://blog.csdn.net/mynameishuangshuai";
        }

    }

    @Override
    public IBinder onBind(Intent arg0) {
        return myBinder;
    }

}

aidl文件

package com.castiel.aidl;
interface CastielProgressConnection{
    String getProName();
}

启动服务

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 启动本地服务和远程服务
        startService(new Intent(this, LocalCastielService.class));
        startService(new Intent(this, RemoteCastielService.class));
    }

通过以上代码可以看出,双进程守护实现代码非常简单,两个服务相互连接,Local服务连接着Remote服务,Remote服务又连接着Local服务,你中有我我中有你,一旦两个服务发现对方被杀掉,另一服务立刻会启动并连接它,下面我给大家演示一下。

这里写图片描述

查看一下系统打印日志:

这里写图片描述

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消