APP中一种在Java层实现的简单守护进程方式
MaiB60
8年前
<p style="text-align:center"><img src="https://simg.open-open.com/show/d305cd6b431d429d35b27b4097ebb142.gif"></p> <p style="text-align:center">CB08F567-698E-46DF-970B-D178FB3BE55B154112-fbbdee0572ec27c6.gif</p> <p>守护进程是一个黑色地带的产物,无论是通过native的方式在linux中fork进程达到,还是在java层通过两个service守护的方式,都是不太友好的做法,据很多人反应,总有一些实际的业务场景中,希望自己的应用保持live状态, 一种是在native中做:</p> <ul> <li> <p>linux中多进程;</p> </li> <li> <p>unix domain套接字实现跨进程通信;</p> </li> <li> <p>linux的信号处理;</p> </li> <li> <p>exec函数族的用法;</p> </li> </ul> <p>把他们组合起来实现了一个双进程守护,几个实现双进程守护时的关键点:</p> <p>父进程如何监视到子进程(监视进程)的死亡?很简单,在linux中,子进程被终止时,会向父进程发送SIG_CHLD信号,于是我们可以安装信号处理函数,并在此信号处理函数中重新启动创建监视进程;</p> <p>子进程(监视进程)如何监视到父进程死亡?当父进程死亡以后,子进程就成为了孤儿进程由Init进程领养,于是我们可以在一个循环中读取子进程的父进程PID,当变为1就说明其父进程已经死亡,于是可以重启父进程。这里因为采用了循环,所以就引出了之前提到的耗电量的问题。</p> <p>父子进程间的通信有一种办法是父子进程间建立通信通道,然后通过监视此通道来感知对方的存在,这样不会存在之前提到的耗电量的问题,在本文的实现中,为了简单,还是采用了轮询父进程PID的办法,但是还是留出了父子进程的通信通道,虽然暂时没有用到,但可备不时之需!</p> <p>今天介绍下用两个service守护的方式作一完整的小案例。仅作学习交流之用。两个进程互相监视对方,发现对方挂掉就立刻重启!(实际就是在onDisconnected时,start另一个service)</p> <p>假设我们的APP中开启了两个Service,分别是A和B,那么:如果A守护B,则B挂掉的同时,A就应该把B唤醒起来,反之亦然,也就是说A和B应该是互相守护,无论谁被杀掉,对方就把它唤醒起来。既然提到了两个Service,那么这两个Service就不能让它们同处在一个进程中,否则就会被一次性双杀。显然不能在同一个进程中,在android中通常我们可以使用AIDL来实现IPC实现。</p> <p>原理图(简单版):</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/54459be4249e890f2686e9e32cb96ffe.jpg"></p> <p style="text-align:center">226BBD66-B17C-4E9C-8849-AE8C6393BB37.png</p> <ul> <li> <h3><strong>MainActivity</strong></h3> <pre> <code class="language-java">public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 启动两个守护服务 startService(new Intent(this, ServiceA.class)); startService(new Intent(this, ServiceB.class)); } }</code></pre> </li> <li> <h3><strong>manifest</strong></h3> <pre> <code class="language-java"><service android:name=".ServiceA"></service> <service android:name=".ServiceB" android:process="com.guardprocess.remote"></service></code></pre> </li> <li> <h3><strong>aide</strong></h3> </li> </ul> <pre> <code class="language-java">interface IBridgeInterface { String getName(); }</code></pre> <ul> <li> <h3><strong>ServiceA</strong></h3> </li> </ul> <pre> <code class="language-java">public class ServiceA extends Service { private static final String TAG = ServiceA.class.getSimpleName(); private MyBinder mBinder; private PendingIntent mPendingIntent; private MyServiceConnection mServiceConnection; @Override public void onCreate() { super.onCreate(); if (mBinder == null) { mBinder = new MyBinder(); } mServiceConnection = new MyServiceConnection(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { this.bindService(new Intent(this, ServiceB.class), mServiceConnection, Context.BIND_IMPORTANT); mPendingIntent = PendingIntent.getService(this, 0, intent, 0); Notification.Builder builder = new Notification.Builder(this); builder.setTicker("守护服务A启动中") .setContentText("我是来守护B不被杀的!") .setContentTitle("守护服务A") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(mPendingIntent) .setWhen(System.currentTimeMillis()); Notification notification = builder.build(); // 设置service为前台进程,避免手机休眠时系统自动杀掉该服务 startForeground(startId, notification); return START_STICKY; } class MyServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder binder) { Log.i(TAG, "ServiceA连接成功"); Toast.makeText(ServiceA.this, "ServiceA连接成功", Toast.LENGTH_LONG).show(); } @Override public void onServiceDisconnected(ComponentName componentName) { // 连接出现了异常断开了,RemoteService被杀掉了 Toast.makeText(ServiceA.this, "ServiceA被干掉", Toast.LENGTH_LONG).show(); // 启动ServiceB ServiceA.this.startService(new Intent(ServiceA.this, ServiceB.class)); ServiceA.this.bindService(new Intent(ServiceA.this, ServiceB.class), mServiceConnection, Context.BIND_IMPORTANT); } } class MyBinder extends IBridgeInterface.Stub { @Override public String getName() throws RemoteException { return "ServiceA"; } } @Override public IBinder onBind(Intent intent) { return mBinder; } }</code></pre> <ul> <li> <h3><strong>ServiceB</strong></h3> </li> </ul> <pre> <code class="language-java">public class ServiceB extends Service { private static final String TAG = ServiceB.class.getSimpleName(); private MyBinder mBinder; private PendingIntent mPendingIntent; private MyServiceConnection mServiceConnection; @Override public void onCreate() { super.onCreate(); if (mBinder == null) { mBinder = new MyBinder(); } mServiceConnection = new MyServiceConnection(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { this.bindService(new Intent(this,ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT); mPendingIntent =PendingIntent.getService(this, 0, intent, 0); Notification.Builder builder = new Notification.Builder(this); builder.setTicker("守护服务B启动中") .setContentText("我是来守护A不被杀的!") .setContentTitle("守护服务B") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(mPendingIntent) .setWhen(System.currentTimeMillis()); Notification notification = builder.build(); //设置service为前台进程,避免手机休眠时系统自动杀掉该服务 startForeground(startId, notification); return START_STICKY; } class MyServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder binder) { Log.i(TAG, "ServiceB连接成功"); Toast.makeText(ServiceB.this, "ServiceB连接成功", Toast.LENGTH_LONG).show(); } @Override public void onServiceDisconnected(ComponentName componentName) { // 连接出现了异常断开了,LocalCastielService被杀死了 Toast.makeText(ServiceB.this, "ServiceB被干掉", Toast.LENGTH_LONG).show(); // 启动ServiceA ServiceB.this.startService(new Intent(ServiceB.this, ServiceA.class)); ServiceB.this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT); } } class MyBinder extends IBridgeInterface.Stub { @Override public String getName() throws RemoteException { return "ServiceB"; } } @Override public IBinder onBind(Intent intent) { return mBinder; } }</code></pre> <p>最后:如果系统干掉这个服务,还是难逃此劫的。向ROM厂商提出加白名单方式,才是终极最万全方案。</p> <p> </p> <p> </p> <p>来自:http://www.jianshu.com/p/b84efa4d6dc0</p> <p> </p>