Android双进程守护

我们以技术不可耻的原则来讲下双进程守护保活应用,达到类似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);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值