当前位置: 移动技术网 > IT编程>移动开发>Android > Android学习之介绍Binder的简单使用

Android学习之介绍Binder的简单使用

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

小型洗衣粉生产设备,周美青 马英九,受诅碾压者胸甲

前言

最近因为公司项目需求,需要远程调度启动客户端输入法输入内容。


这就是大致的需求流程,这篇首先讲远程与服务控制端通讯。首先控制服务端定义好一个service,且在servicemanager注册添加服务。

在这里我讲解远程端与服务控制端通讯(主要通过c++往servicemanager注册服务)。

首先我们得获取到服务控制端注册在servicemanager的服务ibinder对象,通过java反射机制获得ibinder接口对象。

 public static ibinder getremotebinder(){
 try {
 class<?> servicemanager = class.forname("android.os.servicemanager");
 method getservice = servicemanager.getmethod("getservice", string.class);
 ibinder ibinder = (ibinder) getservice.invoke(servicemanager.newinstance(), "inputservice");

 if(ibinder==null){
 log.e(pinyinime.tag,"getservice inputservice : is empty");
 printserverlist();//打印系统所提供的所有服务
 }
 return ibinder;
 } catch (classnotfoundexception e) {
 e.printstacktrace();
 } catch (nosuchmethodexception e) {
 e.printstacktrace();
 } catch (illegalaccessexception e) {
 e.printstacktrace();
 } catch (illegalargumentexception e) {
 e.printstacktrace();
 } catch (invocationtargetexception e) {
 e.printstacktrace();
 } catch (instantiationexception e) {
 e.printstacktrace();
 }
 return null;
 }
//具体源码在android.os.servicemanager
 /**
 * returns a reference to a service with the given name.
 * 
 * @param name the name of the service to get
 * @return a reference to the service, or <code>null</code> if the service doesn't exist
 */
 public static ibinder getservice(string name) {
 try {
 ibinder service = scache.get(name);
 if (service != null) {
 return service;
 } else {
 return getiservicemanager().getservice(name);
 }
 } catch (remoteexception e) {
 log.e(tag, "error in getservice", e);
 }
 return null;
 }

获取到ibinder对象作用是跨进程,举个例子,输入法程序是怎么和应用编辑框通讯的呢?怎么通过什么控制输入法弹起隐藏的呢。也是通过这个ibinder来通讯的,不信你翻翻源码,这里不做详细介绍。

而服务控制端则是由c++层注入服务:

class iservicemanager : public iinterface
{
public:
 declare_meta_interface(servicemanager);

 /**
 * retrieve an existing service, blocking for a few seconds
 * if it doesn't yet exist.
 */
 virtual sp<ibinder> getservice( const string16& name) const = 0;

 /**
 * retrieve an existing service, non-blocking.
 */
 virtual sp<ibinder> checkservice( const string16& name) const = 0;

 /**
 * register a service.
 */
 virtual status_t addservice( const string16& name,
   const sp<ibinder>& service,
   bool allowisolated = false) = 0;

 /**
 * return list of all existing services.
 */
 virtual vector<string16> listservices() = 0;

 enum {
 get_service_transaction = ibinder::first_call_transaction,
 check_service_transaction,
 add_service_transaction,
 list_services_transaction,
 };
};
//上面c++层注册服务提供一个ibinder接口子类,需要实现ontransact方法
 virtual status_t ontransact(uint32_t code,
 const parcel& data,
 parcel* reply,
 uint32_t flags = 0)
 {
 logd("enter myservice ontransact and the code is %d", code);
 switch (code)
 {
 case binder_handle:
 logd("myservice interface handle");
 reply->writecstring("handle reply");
 break;
 case binder_set_screen:
 logd("myservice interface set screen");
 reply->writecstring("set screen reply");
 break;
 case binder_set_char:
 {//call cb
 logd("myservice interface set char before");
 reply->writecstring("set char reply");
 cb = data.readstrongbinder();
 if (cb != null)
 {
 logd("myservice interface set char : %s", data.readcstring());
 parcel in, out;
 in.writeinterfacetoken(string16(binder_name));
 in.writeint32(n++);
 in.writestring16(string16("this is a string."));
 cb->transact(1, in, &out, 0);
 show();
 }
 break;
 }
 default:
 return bbinder::ontransact(code, data, reply, flags);
 }
 return 0;
 }

这样我们可以通过刚刚获取到ibinder对象与之通讯了,这里我只讲个例子:

当远程端设备输入法激活的时候,我将传递输入法输入类型和输入法展示的多功能键传递给服务控制端。

 //当输入法被激活的时候,会调用onstartinputview(editorinfo,boolean)
 parcel data = parcel.obtain();
 data.writeint(editorinfo.inputtype);
 data.writeint(editorinfo.imeoptions);

 log.d(tag, "isactives:" + isactives);

 if (isactives) {
 if (mcontroller != null) {
 mcontroller.startinput(data, parcel.obtain());
 } else {
 isneedactives = true;
 tmp = data;
 mcontroller = new controller(remotebinder,this);
 }
 } else {
 isneedactives = true;
 tmp = data;
 if (mcontroller != null) {
 mcontroller.serviceconnection();
 } else {
 mcontroller = new controller(remotebinder,this);
 }
 }

//这里我将两个int参数写入到parce对象中开始进行通讯

/**
 * 开始输入
 * 
 * @param data
 * 写入输入类型和多功能
 * @param reply
 */
 public void startinput(final parcel data, final parcel reply) {
 log.d(pinyinime.tag, getclass().getname() + ":\t startinput");

 if (!pinyinime.isactives) {
 log.d(pinyinime.tag, "not yet check success , start input failure");
 dealhandler.sendemptymessage(constant.hander_relink);
 return;
 }

 new thread(new runnable() {
 @override
 public void run() {
 if (remotebinder != null && remotebinder.isbinderalive()) {
  try {
  if (remotebinder.transact(
  constant.input_method_activation, data, reply,
  ibinder.flag_oneway)) {
  pinyinime.isneedactives = false;
  log.d(pinyinime.tag,
   "input method to activate, notify the success");
  } else {
  log.d(pinyinime.tag,
   "input method to activate, notify the failure");
  }
  } catch (remoteexception e) {
  e.printstacktrace();
  } finally {
  data.recycle();
  reply.recycle();
  }
 }else{
  dealhandler.sendemptymessage(constant.hander_relink);
 }
 }
 }).start();
 }

这样我们就可以通过获取到的ibinder对象的transact方法进行通讯。

//code必须双方定义好,否则接收数据无法正常,
//第一个是我们装载的序列化数据,
//第二我们可以直接传个对象,最好一个是需要返回结果的标识,
//0代表需要返回内容,flag_oneway单方面无需返回结果的标识

public boolean transact(int code, parcel data, parcel reply, int flags)
 throws remoteexception;

当我们调用了ibinder.transact(int,parce,parce,int)方法,这是注册的服务中的ibinder对象的ontransact(int,parce,parce,int)方法就会被响应,这样我们就实现了远程端跟服务控制端通讯了。

到了这里,有个问题,服务控制端接收到客户端输入的内容咋办,怎通知远程端输入法输入内容到编辑框中呢。

其实也很简单,我们只需要在远程端输入法程序实现一个ibinder对象,传递给服务控制端,这样就可以实现,具体怎么传递了?

//首先我们得让远程输入法程序拥有属于自己的ibinder类。
package com.redfinger.inputmethod.server;

import com.android.inputmethod.pinyin.pinyinime;

import android.annotation.suppresslint;
import android.os.binder;
import android.os.ibinder;
import android.os.iinterface;
import android.os.parcel;
import android.os.remoteexception;
import android.util.log;
import android.view.keyevent;

public interface inputbinder extends iinterface{

 public static class stub extends binder implements inputbinder{
 private static final java.lang.string descriptor = "com.redfinger.inputmethod.service.inputbinder";
 public pinyinime pinyinime;

 public stub(pinyinime pinyinime) {
 this.pinyinime = pinyinime;
 this.attachinterface(this, descriptor);
 }

 public inputbinder asinterface(ibinder obj){
 if(obj == null){
 return null;
 }
 iinterface iinterface = obj.querylocalinterface(descriptor);
 if(iinterface!=null&&iinterface instanceof inputbinder){
 return (inputbinder)iinterface;
 }
 return new stub.proxy(obj);
 }

 @override
 public ibinder asbinder() {
 return this;
 }

 @suppresslint({ "newapi", "recycle" })
 @override
 protected boolean ontransact(int code, parcel data, parcel reply,
 int flags) throws remoteexception {
 switch (code) {
 case constant.connection_handshake2:
 string datastring = data.readstring();
 log.d(pinyinime.tag, "the second handshake start [data = "+datastring +"]");
 if("connection_responed".equals(datastring)){   
  parcel parcel = parcel.obtain();
  parcel.writestring("connection_finish");
  pinyinime.getremotebinder().transact(constant.connection_handshake3, parcel, parcel.obtain(), ibinder.flag_oneway);
  pinyinime.isactives = true;
  log.d(pinyinime.tag, "the third handshake success");

  if (pinyinime.isneedactives) {
  pinyinime.mcontroller.startinput(pinyinime.gettmp(), parcel.obtain());
  }
  if (pinyinime.isneedcloseinputmethod) {
  pinyinime.mcontroller.finishinput();
  }

 }else{
  log.d(pinyinime.tag, "the third handshake failure , agent connect ! ");
  pinyinime.mcontroller.serviceconnection();
 }
 break;
 case constant.function_input:
 ....
 switch (keycode) {
 case 14:
  pinyinime.simulatekeyeventdownup(keyevent.keycode_del);
  return true;
 case 28:
  pinyinime.simulatekeyeventdownup(keyevent.keycode_enter);
  return true;
 case 65:
  pinyinime.requesthideselffromclient = true;
  pinyinime.requesthideself(0);
  break;
 } 
 break;
 case constant.character_input: 
 ....
 return true;
 case constant.disconnection:
 ....
 break;
 case constant.input_method_platform:
 ....
 break;
 }
 return super.ontransact(code, data, reply, flags);
 }

 public static class proxy implements inputbinder{

 private android.os.ibinder mremote;

 public proxy(android.os.ibinder mremote) {
 this.mremote = mremote;
 }
 @override
 public ibinder asbinder() {
 return mremote;
 }
 public java.lang.string getinterfacedescriptor()
 {
 return descriptor;
 }
 }
 static final int receivechar = (android.os.ibinder.first_call_transaction + 0);
 }
}

是不是特变像aidl文件的内容一样,aidl其实就是android自己给我写好的ibinder代码一样。

这样我们就可以在获取到服务控制端ibinder对象中写入我们自己ibinder对象,传递过去让他通过transact方法来与输入法程序ibinder对象通讯了。

 //parce类中提供了这样的一个方法,就是用于写入ibinder对象的。
 public final void writestrongbinder(ibinder val) {
 nativewritestrongbinder(mnativeptr, val);
 }

这样我们就可以在inputbinder类中来处理返回的数据了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网