Android双进程守护

来源:互联网 发布:mac chrome 导出插件 编辑:程序博客网 时间:2024/06/13 21:50
我们以技术不可耻的原则来讲下双进程守护保活应用,达到类似QQ的保活效果。当然QQ保活主要是厂商定制后添加了应用白名单,在底层上就得到了保活的支持。双进程另一个好处就是可以分配更多的内存,我们知道每一个进程都会分配对应的内存,所以当2个进程时会保证应用的流畅行。

1、 创建本地服务和远程服务配置
在 AndroidManifest.xml中配置如下:
需要让远程服务运行在另一个进程中,达到双进程的效果

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

2、 aidl
新建aidl让两个进程通信。创建如下代码:

    // IMyAidlInterface.aidl    package com.hu.test;    // Declare any non-default types here with import statements    interface IMyAidlInterface {    /**     * Demonstrates some basic types that you can use as parameters     * and return values in AIDL.     */      String getServiceName();}

3、 本地服务

package com.hu.test;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.support.annotation.Nullable;import android.widget.Toast;/** * Created by TT on 2017/6/20. */public class LocalService extends Service {    MyConn conn;    MyBinder binder;    @Nullable    @Override    public IBinder onBind(Intent intent) {        return binder;    }    @Override    public void onCreate() {        super.onCreate();        binder =new MyBinder();        conn = new MyConn();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Toast.makeText(this, " 本地服务started", Toast.LENGTH_SHORT).show();        this.bindService(new Intent(this, RemoteService.class), conn, Context.BIND_IMPORTANT);        return START_STICKY;    }    @Override    public void onDestroy() {        super.onDestroy();        //开启本地服务        LocalService.this.startService(new Intent(LocalService.this, RemoteService.class));        //绑定本地服务        LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);    }    class MyBinder extends IMyAidlInterface.Stub {        public String getServiceName() throws RemoteException {            return "LocalService";        }    }    class MyConn implements ServiceConnection {        @Override        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {            Toast.makeText(getApplicationContext(), "远程服务连接L", Toast.LENGTH_SHORT).show();        }        @Override        public void onServiceDisconnected(ComponentName componentName) {            //与本地服务连接失败            Toast.makeText(getApplicationContext(), "远程服务失败", Toast.LENGTH_SHORT).show();            LocalService.this.startService(new Intent(LocalService.this, RemoteService.class));            LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);        }    }}

4、 远程服务

package com.hu.test;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.support.annotation.Nullable;import android.widget.Toast;/** * Created by TT on 2017/6/20. */public class RemoteService extends Service {    MyConn conn;    MyBinder binder;    @Nullable    @Override    public IBinder onBind(Intent intent) {        return binder;    }    @Override    public void onCreate() {        super.onCreate();        binder =new MyBinder();        conn = new MyConn();    }    @Override    public void onStart(Intent intent, int startId) {        super.onStart(intent, startId);        Toast.makeText(this, " 远程服务started", Toast.LENGTH_SHORT).show();        RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);    }    @Override    public void onDestroy() {        super.onDestroy();        //开启本地服务        RemoteService.this.startService(new Intent(RemoteService.this, LocalService.class));        //绑定本地服务        RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);    }    class MyBinder extends IMyAidlInterface.Stub {        public String getServiceName() throws RemoteException {            return "RemoteService";        }    }    class MyConn implements ServiceConnection {        @Override        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {            Toast.makeText(getApplicationContext(), "远程服务连接R", Toast.LENGTH_SHORT).show();        }        @Override        public void onServiceDisconnected(ComponentName componentName) {            //与本地服务连接失败            Toast.makeText(getApplicationContext(), "远程服务失败", Toast.LENGTH_SHORT).show();            RemoteService.this.startService(new Intent(RemoteService.this, LocalService.class));            RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);        }    }}
原创粉丝点击