Android FrameWork 之Binder机制初识

public class  Binder extends Object implements IBinder

IBinder

为远程对象制定了基础的接口方法,轻量级进程内与进程间IPC调用的核心部分。该类为抽象类,并且android建议通过继承 Binder去实现接口,而不是直接的去实现IBinder接口。

IBinder的核心方法是:

    public boolean transact(int code, Parcel data, Parcel reply, int flags)
        throws RemoteException;

它与Binder中的onTransact方法对应,它允许你使用IBinder(客户端)、Binder( 服务端 )交互。

并且当客户端执行完transact()方法后并不会立刻回调,而是等待 服务端 onTransact()执行完毕后才会回调。

 /**
     * Default implementation is a stub that returns false.  You will want
     * to override this to do the appropriate unmarshalling of transactions.
     *
     * <p>If you want to call this, call transact().
     */
    protected boolean onTransact(int code, Parcel data, Parcel reply,
            int flags) throws RemoteException {
        if (code == INTERFACE_TRANSACTION) {
            reply.writeString(getInterfaceDescriptor());
            return true;
        } else if (code == DUMP_TRANSACTION) {
            ParcelFileDescriptor fd = data.readFileDescriptor();
            String[] args = data.readStringArray();
            if (fd != null) {
                try {
                    dump(fd.getFileDescriptor(), args);
                } finally {
                    try {
                        fd.close();
                    } catch (IOException e) {
                        // swallowed, not propagated back to the caller
                    }
                }
            }
            // Write the StrictMode header.
            if (reply != null) {
                reply.writeNoException();
            } else {
                StrictMode.clearGatheredViolations();
            }
            return true;
        }
        return false;
    }

例如,当进程A与进程B 通过IPC通讯。

进程A调用transact()方法,将Parcel发送给进程B,进程B接收到传入的Parcel后,调用Binder.onTransact()方法,并使用Parcel进行回复。在等待进程B回复的过程中,进程A是阻塞的。


绑定服务进行交互的几种方式

1.Extending  the binder class

应用场景

Service 是Applicatin私有的,并且与Application运行在同进程中(比较普遍),因为该方式不支持进程间通讯

创建Binder与Service:

public class LocalService extends Service {
    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();
    // Random number generator
    private final Random mGenerator = new Random();

    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /** method for clients */
    public int getRandomNumber() {
      return mGenerator.nextInt(100);
    }
}
客户端调用:

public class BindingActivity extends Activity {
    LocalService mService;
    boolean mBound = false;

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

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, LocalService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    /** Called when a button is clicked (the button in the layout file attaches to
      * this method with the android:onClick attribute) */
    public void onButtonClick(View v) {
        if (mBound) {
            // Call a method from the LocalService.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
            int num = mService.getRandomNumber();
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;// 获取本地的IBinder对象句柄
            mService = binder.getService();//通过IBinder句柄,获取Service的句柄
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}

2.Use Messenger

应用场景:

使用消息驱动,支持IPC(Interface Progress Communication)。

===Handler异步消息处理线程详解===

示例代码:

public class MessengerService extends Service {
    /** Command to the service to display a message */
    static final int MSG_SAY_HELLO = 1;

    /**
     * Handler of incoming messages from clients.
     */
    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_SAY_HELLO:
                    Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    /**
     * Target we publish for clients to send messages to IncomingHandler.
     */
    final Messenger mMessenger = new Messenger(new IncomingHandler());

    /**
     * When binding to the service, we return an interface to our messenger
     * for sending messages to the service.
     */
    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
        return mMessenger.getBinder();
    }
}

客户端调用:

public class ActivityMessenger extends Activity {
    /** Messenger for communicating with the service. */
    Messenger mService = null;

    /** Flag indicating whether we have called bind on the service. */
    boolean mBound;

    /**
     * Class for interacting with the main interface of the service.
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the object we can use to
            // interact with the service.  We are communicating with the
            // service using a Messenger, so here we get a client-side
            // representation of that from the raw IBinder object.
            mService = new Messenger(service);//如果服务连接成功,则将服务端IBinder对象传递至客户端,mMessenger.getBinder();的值也是这里被初始化的;
            mBound = true;//这样做的好处是,1)将IBinder的处理封装在Messenger类中,2)由IPC调用转为本地的异步线程处理
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mService = null;
            mBound = false;
        }
    };

    public void sayHello(View v) {
        if (!mBound) return;
        // Create and send a message to the service, using a supported 'what' value
        Message msg = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
        try {
            mService.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

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

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to the service
        bindService(new Intent(this, MessengerService.class), mConnection,
            Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }
}

3.Use AIDL (Android Interface Define Language)

Android本身并不推荐使用AIDL,因为它需要支持多线程,使用它会使应用变得更复杂。

关于AIDL接口的定义:请查阅AIDL

操作步骤:

1.新建xxx.aidl文件,定义接口;

2.Android AIDL工具将自动编译在Gen目录下生成对应的Java文件(使用Eclipse开发环境)。

package com.example.android;
interface IServerServiceAidl{
void startServer(String command);
void stopServer();
}


实际上完全可以手动去实现这部分内容(如下面),

相较于使用AIDL在Gen目录下自动生成的JAVA文件来说,本质上并无区别;

public interface IServerServiceAidl extends IInterface {
	/** Local-side IPC implementation stub class. */

	public void startServer(String command) throws RemoteException;

	public void stopServer() throws RemoteException;
}

public class Proxy implements IServerServiceAidl {
	private IBinder mRemote;

	Proxy(IBinder remote) {
		mRemote = remote;
	}

	@Override
	public IBinder asBinder() {
		return mRemote;
	}

	public String getInterfaceDescriptor() {
		return Stub.DESCRIPTOR;
	}

	@Override
	public void startServer(String command) throws RemoteException {
		Parcel _data = Parcel.obtain();
		Parcel _reply = Parcel.obtain();
		try {
			_data.writeInterfaceToken(Stub.DESCRIPTOR);
			_data.writeString(command);
			mRemote.transact(Stub.TRANSACTION_STARTSERVER, _data, _reply, 0);
			_reply.readException();
		} finally {
			_reply.recycle();
			_data.recycle();
		}
	}

	@Override
	public void stopServer() throws RemoteException {
		Parcel _data = Parcel.obtain();
		Parcel _reply = Parcel.obtain();
		try {
			_data.writeInterfaceToken(Stub.DESCRIPTOR);
			mRemote.transact(Stub.TRANSACTION_STOPSERVER, _data, _reply, 0);
			_reply.readException();
		} finally {
			_reply.recycle();
			_data.recycle();
		}
	}
}

public abstract class Stub extends Binder implements IServerServiceAidl {
	public static final String DESCRIPTOR = "IServerServiceAidl";
	public static final int TRANSACTION_STARTSERVER = (IBinder.FIRST_CALL_TRANSACTION + 0);
	public static final int TRANSACTION_STOPSERVER = (IBinder.FIRST_CALL_TRANSACTION + 1);

	/** Construct the stub at attach it to the interface. */
	public Stub() {
		this.attachInterface(this, DESCRIPTOR);
	}

	/**
	 * Cast an IBinder object into an IServerServiceAidl interface, generating a
	 * proxy if needed.
	 */
	public static IServerServiceAidl asInterface(IBinder obj) {
		if ((obj == null)) {
			return null;
		}
		IInterface iin = obj.queryLocalInterface(DESCRIPTOR);

		if (((iin != null) && (iin instanceof IServerServiceAidl))) {
			Log.d("debug", "Return :Local Binder Object");
			return ((IServerServiceAidl) iin);
		}
		Log.d("debug", "Return :Remote Binder Proxy Object");
		return new Proxy(obj);
	}

	@Override
	public IBinder asBinder() {
		return this;
	}

	@Override
	public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
			throws RemoteException {
		switch (code) {
		case INTERFACE_TRANSACTION: {
			reply.writeString(DESCRIPTOR);
			return true;
		}
		case TRANSACTION_STARTSERVER: {
			data.enforceInterface(DESCRIPTOR);
			String _arg0;
			_arg0 = data.readString();
			this.startServer(_arg0);
			reply.writeNoException();
			return true;
		}
		case TRANSACTION_STOPSERVER: {
			data.enforceInterface(DESCRIPTOR);
			this.stopServer();
			reply.writeNoException();
			return true;
		}
		}
		return super.onTransact(code, data, reply, flags);
	}

}
这里当使用到Binder时,将会先判断是否是本地的Binder,如果是本地的就直接使用了没有必要去远程ServiceManager去索取;

接下来就是通过Parcel交互数据的过程,这里需要校验下DESCRIPTOR,来判断请求者是否是真的想请求指定服务程序。


附上一些扩展阅读资料以帮助更好的理解Binder:

Android Binder RPC机制了解

Android Binder设计与实现 – 设计篇

Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
千里马8年Android系统及应用开发经验,曾担任过美国unokiwi公司移动端技术总监兼架构师,对系统开发,性能优化,应用高级开发有深入的研究,Android开源定制ROM Lineage的贡献者之一,国内首家线下开辟培训Android Framework课程,拥有2年的Android系统培训经验。成为腾讯课堂专业负责android framework课程分享第一人,致力于提高国内android Framework水平Android Framework领域内是国内各大手机终端科技公司需要的人才,应用开发者都对Android系统充满着好奇,其中的binder是重中之重,都说无binderAndroidbinde是Android系统的任督二脉。课程水平循序渐进,由中级再到高级,满足各个层次水平的android开发者。1、灵活使用binder跨进程通信,在app端对它的任何api方法等使用自如2、可以单独分析android系统源码中任何binder部分,分析再也没有难度3、掌握binder驱动本质原理,及对应binder驱动怎么进行跨进程通信,及内存等拷贝方式数据等4、对binder从上层的java app端一直到最底层的内核binder驱动,都可以顺利理通5、针对系统开发过程中遇到的binder报错等分析方法,及binder bug案例学习6、针对面试官任何的binder问题都可以对答自如7、socket这种跨进程通信实战使用8、针对android源码中使用的socket源码轻松掌握9、android系统源码中最常见的socketpair中双向跨进程通信10、使用socket实现一个可以让app执行shell命令的程序

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值