当前位置: 移动技术网 > IT编程>移动开发>Android > Android基于Aidl的跨进程间双向通信管理中心

Android基于Aidl的跨进程间双向通信管理中心

2019年08月01日  | 移动技术网IT编程  | 我要评论

得益于最近有点时间和精力,我想起来了一件事。那就是在上家公司,公司要求做一个app进程间的通信的功能,并不是app对app的直接跨进程通信,而是通过一个服务中心,做接收,然后,再转发,避免应用之间耦合性高,不然的话,新增一个app,其他app也要进行升级更新(类似于有服务中心的聊天室)。

我就花几个小时写点东西吧,顺便记录一下

大家都知道在android设备上,有很多方式,比如,广播,socket,共享内存,aidl等,其中广播和aidl都是基于android中ibinder机制

广播:

广播有缺陷,就是效率不高,有时候会遇到广播丢失,或者说广播的队列过长,导致消息发送慢;

共享内存:

共享内存没有安全性可言,而且多线程读写数据的话,会无法控制

socket:

socket耦合度较高,内存需要拷贝两次,适用于跨网络

aidl:

基于binder,效率高;基于c/s架构,分层清晰,功能明确;有linux的进程id概念,更加安全等优点

流程图

很简单的架构,所有的app消息传递都通过server来做,工程结构如下,center(消息中心),app1,app2都依赖于lib(aidl接口库)

利用aidl中的remotecallbacklist类(原理和源码我就不多说了,其实client调用server是大同小异的,只不过是反者来了一次),来实现client中的接口回调,这样才能从server主动给client发消息,一般我们都是client主动调用server,现在轮到server主动调用client

服务端的代码如下,你可以按照你项目的要求来做

package com.helang.messagecenterdemo;
 
import android.app.service;
import android.content.intent;
import android.os.ibinder;
import android.os.remotecallbacklist;
import android.os.remoteexception;
import android.support.annotation.nullable;
import android.util.log;
 
import com.helang.lib.imyaidlcallbackinterface;
import com.helang.lib.imyaidlinterface;
 
/**
 * 消息服务中心(记得在 manifest.xml 加上 android:exported="true")
 */
public class myservice extends service {
  private final static string tag = myservice.class.getsimplename();
  private remotecallbacklist<imyaidlcallbackinterface> callbacklist = new remotecallbacklist<>();//回调的关键(api>=17,才能使用)
 
  @override
  public void oncreate() {
    super.oncreate();
  }
 
  @nullable
  @override
  public ibinder onbind(intent intent) {
    return ibinder;
  }
 
  /**
   * 实现ibinder
   */
  private imyaidlinterface.stub ibinder = new imyaidlinterface.stub() {
    @override
    public void sendmessage(string tag, string message) throws remoteexception {
      callbacklist.beginbroadcast();
      sendmessagetoallclient(tag,message);
      log.d(tag,"tag="+tag+" message="+message);
      callbacklist.finishbroadcast();
    }
 
    @override
    public void registerlistener(imyaidlcallbackinterface listener) throws remoteexception {
      callbacklist.register(listener);//注册回调listener
      log.d(tag,"registerlistener");
    }
 
    @override
    public void unregisterlistener(imyaidlcallbackinterface listener) throws remoteexception {
      callbacklist.unregister(listener);//取消回调listener
      log.d(tag,"unregisterlistener");
    }
  };
 
  /**
   * 发送消息给全部的client(你也可以指定发送给某个client,也可
   * 以根据自己的业务来封装一下bean,记得要实现parcelable接口来序列化
   * @param tag
   * @param message
   */
  private void sendmessagetoallclient(string tag,string message){
    for (int i = 0 ; i < callbacklist.getregisteredcallbackcount();i++){
      try {
        callbacklist.getbroadcastitem(i).callback(tag,message);
      } catch (remoteexception e) {
        e.printstacktrace();
      }
    }
  }
}

client1和client2代码是一样的,就是相互发消息:

package com.helang.app2;
 
import android.content.componentname;
import android.content.intent;
import android.content.serviceconnection;
import android.os.handler;
import android.os.ibinder;
import android.os.remoteexception;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
import android.widget.textview;
 
import com.helang.lib.imyaidlcallbackinterface;
import com.helang.lib.imyaidlinterface;
 
public class mainactivity extends appcompatactivity {
  private edittext edittext;
  private button bt_send;
  private textview text;
 
  private imyaidlinterface imyaidlinterface;
  private servicecallback servicecallback;
 
  private myserviceconnection myserviceconnection;
 
  private handler handler = new handler();
 
 
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    bt_send = findviewbyid(r.id.bt_send);
    edittext = findviewbyid(r.id.edittext);
    text = findviewbyid(r.id.text);
 
    bt_send.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view view) {
        if (imyaidlinterface != null){
          try {
            imyaidlinterface.sendmessage("app2",edittext.gettext().tostring().trim());
          } catch (remoteexception e) {
            e.printstacktrace();
          }
        }
      }
    });
 
    bindservice();
  }
 
  @override
  protected void ondestroy() {
    super.ondestroy();
    unbindservice();
  }
 
  private void bindservice(){
    myserviceconnection = new myserviceconnection();
    servicecallback = new servicecallback();
    intent intent = new intent();
    intent.setcomponent(new componentname("com.helang.messagecenterdemo",
        "com.helang.messagecenterdemo.myservice"));
    startservice(intent);//开启远程服务
    bindservice(intent,myserviceconnection,bind_auto_create);//绑定服务
 
  }
 
  private void unbindservice(){
    if (myserviceconnection != null){
      try {
        imyaidlinterface.unregisterlistener(servicecallback);
      } catch (remoteexception e) {
        e.printstacktrace();
      }
      unbindservice(myserviceconnection);
    }
  }
 
  /**
   * 连接service
   */
  class myserviceconnection implements serviceconnection {
 
    @override
    public void onserviceconnected(componentname componentname, ibinder ibinder) {
      imyaidlinterface = imyaidlinterface.stub.asinterface(ibinder);
 
      handler.post(new runnable() {
        @override
        public void run() {
          //注册回调
          if (imyaidlinterface != null){
            try {
              imyaidlinterface.registerlistener(servicecallback);
            } catch (remoteexception e) {
              e.printstacktrace();
            }
          }
        }
      });
 
    }
 
    @override
    public void onservicedisconnected(componentname componentname) {
 
    }
  }
 
  /**
   * service回到client的类
   */
  class servicecallback extends imyaidlcallbackinterface.stub{
 
    @override
    public void callback(final string tag, final string message) throws remoteexception {
      runonuithread(new runnable() {
        @override
        public void run() {
          text.append("tag="+tag+" message="+message);
        }
      });
    }
  }
}

看看效果吧,client2(app2)发消息给client1(app1)

顺便说一句,提前打开center服务,因为android 8.0之后的版本直接远程开启其他app后台进程服务,是行不通了,可以绑定一个前台进程,网上方法有很多,我这里就简单处理了

源码我都放在github:messagecenter

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网