这里主要记录下学习AIDL,通过AIDL来实现一个伪守护进程,也就是杀不死的进程(ps:这里指的是在一定情况下是杀不死,离微信,qq那些用NDK来实现的差很远,只是用于学习)。

先看下效果图

android 双守护进程jar 编写android守护进程_ide

看了效果图是不是发现我这个demo里面有两个进程,杀死一个还能重启另外一个呢!下面主要记录下在应用层通过AIDL 和 Service来实现一个伪守护进程。看下面代码:

要实现守护我们是不是得启动2根服务来监听,如微信,360它们的应用都不会少于2根线程在后台运行,进行相互监听。那我们就先启动2根服务

MainActivity代码:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent localService = new Intent(this, LocalService.class);
        this.startService(localService);

        Intent remoteService = new Intent(this, RemoteService.class);
        this.startService(remoteService);
    }

}

Manifest配置

<service android:name=".LocalService" />

      <service android:name=".RemoteService" android:process=":remote"/>

设置AIDL接口,新建一个.aidl为后缀的aidl文件,我这里随便提供了一个方法

package com.richard.guardservicedemo.aidl;

interface GuardAidl{
    void doSomething();
}

LocalService代码:

package com.richard.guardservicedemo;

import com.richard.guardservicedemo.aidl.GuardAidl;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

/**
 * 本地服务
 * @author Richard
 *
 */
public class LocalService extends Service{

    private MyBilder mBilder;

    @Override
    public IBinder onBind(Intent intent) {
        if (mBilder == null)
            mBilder = new MyBilder();

        return mBilder;

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        //在LocalService运行后,我们对RemoteService进行绑定。 把优先级提升为前台优先级
        this.bindService(new Intent(LocalService.this, RemoteService.class), 
                connection, Context.BIND_ABOVE_CLIENT);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        return super.onStartCommand(intent, flags, startId);
    }

    private class MyBilder extends GuardAidl.Stub{

        @Override
        public void doSomething() throws RemoteException {
            Log.i("TAG", "绑定成功!");
            Intent localService = new Intent(LocalService.this, RemoteService.class);
            LocalService.this.startService(localService);
            LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), 
                    connection, Context.BIND_ABOVE_CLIENT);
        }

    }

    private ServiceConnection connection = new ServiceConnection() {

        /**
         * 在终止后调用,我们在杀死服务的时候就会引起意外终止,就会调用onServiceDisconnected
         * 则我们就得里面启动被杀死的服务,然后进行绑定
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("TAG", "RemoteService被杀死了");
            Intent localService = new Intent(LocalService.this, RemoteService.class);
            LocalService.this.startService(localService);
            LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), 
                    connection, Context.BIND_ABOVE_CLIENT);
            Toast.makeText(LocalService.this, "RemoteService被杀死!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("TAG", "RemoteService链接成功!");
            try {
                if(mBilder != null)
                    mBilder.doSomething();
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };


}

RemoteService代码

package com.richard.guardservicedemo;

import com.richard.guardservicedemo.aidl.GuardAidl;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

/**
 * 守护服务
 * @author Richard
 *
 */
public class RemoteService extends Service {

    private MyBilder mBilder;

    @Override
    public IBinder onBind(Intent intent) {
        if (mBilder == null)
            mBilder = new MyBilder();
        return mBilder;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        //在RemoteService运行后,我们对LocalService进行绑定。 把优先级提升为前台优先级
        this.bindService(new Intent(RemoteService.this, LocalService.class),
                connection, Context.BIND_ABOVE_CLIENT);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    private class MyBilder extends GuardAidl.Stub{

        @Override
        public void doSomething() throws RemoteException {
            Log.i("TAG", "绑定成功!");
        }

    }

    private ServiceConnection connection = new ServiceConnection() {

        /**
         * 在终止后调用,我们在杀死服务的时候就会引起意外终止,就会调用onServiceDisconnected
         * 则我们就得里面启动被杀死的服务,然后进行绑定
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("TAG", "LocalService被杀死了");
            Intent remoteService = new Intent(RemoteService.this, LocalService.class);
            RemoteService.this.startService(remoteService);
            RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class),
                    connection, Context.BIND_ABOVE_CLIENT);
            Toast.makeText(RemoteService.this, "LocalService被杀死!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("TAG", "LocalService链接成功!");
            try {
                if(mBilder != null)
                    mBilder.doSomething();
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

}

到这里基本上就实现了一个伪守护进程,’杀不死应用’。呵呵,当然这还是可以杀死的。在应用层如果还想保险点,那就加广播监听,比如:电量变化的广播啊!
当然,不推荐这样写代码。因为这不是正确的实现方式,也不是保险的实现方式,这只是一个然并卵的实现方式。不推荐用于实际开发。