[Android]用AIDL生成Service

    对于用aidl生成Service,之前作过,这里记录一下,顺便也整理一下思路。java

    一、建立aidl文件。android

    这个aidl文件必须包括包名,和用interface的形式定义方法。eclipse

    例如:IAIDLService.aidl。异步

package com.ting.androidexample.services;

interface IAIDLExampleService {
	void show();
	void function(String packageName, IBinder binder);
}

     由于我是在eclipse中开发,因此这时会在gen目录下自动生成IAIDLExampleService.java文件。 ide

    

    二、建立AIDLService.java文件。code

    这个文件是一个Service,所以要继承Service类,由于Service是抽象类,所以,要实现抽象的onBind方法。onBind方法的返回值是一个Binder,这个Binder是要传给客户端的。这个Binder要继承自由aidl文件自动生成的IAIDLService.java类中的抽象子类Stub。而这个抽象子类中未实现的方法就是咱们在IAIDLService.aidl中定义的方法。继承

public class AIDLService extends Service {
	
	private final IAIDLService.Stub mBinder = new IAIDLService.Stub() {

		@Override
		public void show() throws RemoteException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void function(String packageName, IBinder binder)
				throws RemoteException {
			// TODO Auto-generated method stub
			
		}
		
	};

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

    对于mBinder中的方法会传给绑定它的组件,而后绑定它的组件会调用这些方法。

    三、建立与AIDLService通讯的类,好比,咱们先用Activity去调用与该Service通讯。ip

public class AIDLActivity extends Activity {
	
	private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = new Intent();
		bindService(intent, conn, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindService(conn);
	}	
}

    只有执行bindService和unbindService时才会用到AIDL这种方式。

    在执行bindService时,它的第二个参数是一个ServiceConnection实例,这个实例有两个回调方法,onServiceConnected是服务链接上的回调,onServiceDisconnected是服务断开链接的回调。因此说,bindService在链接时是异步的。对于onServiceConnected的第二个参数就是Service返回给Activity的binder。也就是靠它来通讯的。Activity能够经过这个binder调用传过来的方法。开发

    那么AIDLActivity如何使用这个传过来的Binder呢?咱们看下面的代码。get

private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			 mIAIDLService = IAIDLService.Stub.asInterface(service);
			 mIAIDLService.show();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mIAIDLService = null;
		}
		
	};

    Activity的ServiceConnection中的代码能够修改成上面这样。首先获得mIAIDLService实例,而后就能够用mIAIDLService来执行aidl文件中定义的方法。这里须要注意,aidl文件中定义的方法能够是Binder,好比IAIDLService.aidl中的function方法的第二个参数就是Binder。对于Activity来讲,它没有办法得到这个Binder传到参数中。

    若是绑定AIDLService的是一个Service,咱们定义为ClientService的话,而且这个 ClientService 有一些方法须要传给AIDLService,那么就能够为这个 ClientService建立IClientService.aidl文件,而且把须要的方法定义在aidl文件中。这样,在ClientService中就能够实例化一个它本身的Binder,而且能够经过上面IAIDLService的function方法把这个Binder传入。

    例如:

public class ClientService extends Service {
	
	private IAIDLService mIAIDLService;
	
	private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			 mIAIDLService = IAIDLService.Stub.asInterface(service);
			 try {
				mIAIDLService.show();
				mIAIDLService.function(getPackageName(), mBinder);
			} catch (RemoteException e) {
				e.printStackTrace();
			}		 
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mIAIDLService = null;
		}
		
	};
	
	private final IClientService.Stub mBinder = new IClientService.Stub() {
		
		@Override
		public void test() throws RemoteException {
			// TODO Auto-generated method stub
			
		}
	};
		
	@Override
	public void onCreate() {
		super.onCreate();
		Intent intent = new Intent();
		bindService(intent, conn, Context.BIND_AUTO_CREATE);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		unbindService(conn);
	}	

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

}

    IClientService.aidl文件为:

package com.ting.androidexample.services;

interface IClientService {
	void test();
}
相关文章
相关标签/搜索