您的位置:首页 > 移动开发 > Android开发

android进程间通信(远程服务aidl,客户端)

2015-03-01 15:47 330 查看
上一篇已经介绍完服务端的创建,在此基础上创建客户端来实现进程间通信。

客户端



可以看到客户端的包(com.hm.aidl)下的文件和服务端的一样,文件和包名一定要相同。

布局文件很简单:四个button,负责bind服务,注册回调,获取数据

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_bind_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickButton"
        android:text="Bind Service" />

    <Button
        android:id="@+id/btn_register_callback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickButton"
        android:text="Register Callback" />

    <Button
        android:id="@+id/btn_get_current_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickButton"
        android:text="Get Current Data" />

    <Button
        android:id="@+id/btn_get_datas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickButton"
        android:text="Get Datas" />

</LinearLayout>


客户端绑定服务使用ServiceConnection

ServiceConnection mServiceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName componentName) {
			Log.d(TAG, "onServiceDisconnected");
			mService = null;
		}

		@Override
		public void onServiceConnected(ComponentName componentName, IBinder binder) {
			Log.d(TAG, "onServiceConnected");
			mService = IService.Stub.asInterface(binder);
		}
	};


上一篇文章提到服务端回调客户端需要一个IBinder,这里是ICallback

ICallback mCallback = new ICallback.Stub() {

		@Override
		public void notify(String action, Bundle bundle) throws RemoteException {
			Log.d(TAG, "notify : " + action);
		}
	};


bindService使用ServiceConnection

private void bindService() {
		Intent intent = new Intent();
		intent.setAction("android.intent.action.HM_SERVICE");
		bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
		Log.d(TAG, "bindService");
	}


注册回调,获取数据

private void registerCallback() {
		if (mService != null) {
			try {
				mService.registerCallback(mCallback);
				Log.d(TAG, "registerCallback");
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	private void getCurrentData() {
		if (mService != null) {
			try {
				EntityData data = mService.getCurrentData();
				if (data != null) {
					Log.d(TAG, "getCurrentData : id = " + data.id + " name = " + data.name);
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	private void getDatas() {
		if (mService != null) {
			try {
				List<EntityData> datas = mService.getDatas();
				if (datas != null) {
					Log.d(TAG, "getDatas : size = " + datas.size());
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}


先运行服务端,再来启动客户端,下面来看通信的log信息:

先绑定服务



注册回调:这个时候其实服务端已经开始回调客户端了



获取数据



以上就可以实现客户端和服务端之间的双向通信了。

代码链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: