跨进程调用service(AIDL)

在过去的技术中,Corba可以实现跨进程的调用;在Java技术中,RMI也可以实现跨进程的调用;在Android中类似可以使用AIDL服务跨进程调用Service。

Android的远程Service调用与Java的RMI基本相似,一样都是先定义一个远程调用接口,然后为该接口提供一个实现类即可。与RMI不同的是,客户端访问Service时,Android并不是直接返回Service对象给客户端。

在Android中绑定本地Service时,返回的是一个IBinder对象;而访问远程Service时,是将IBinder的代理返回给客户端,当客户端获得IBinder对象的代理后,就可以通过IBinder对象去回调远程Service的属性和方法了。

示例如下:

创建AIDL文件:

AIDL:指Android Interface Definition Language

注意:AIDL定义接口的源代码必须以.aidl结尾;

AIDL接口中用到的数据类型,除了基本类型、String、List 、Map、CharSequence之外,其他类型都需要导包,即使在同一包中也需导包。

在Service端和客户端都需要为该接口提供实现。使用ADT工具进行开发,会自动为AIDL接口生成实现。

这是AIDL源代码的一个简单例子:

  1. package com.lovo.service;  
  2. interface IMessage{  
  3.     int getFlag();  
  4.     String getMessage(String msg);  
  5. }  
package com.lovo.service;
interface IMessage{
	int getFlag();
	String getMessage(String msg);
}

自动生成的AIDL实现,在此不给出。


远程的Service:

  1. package com.lovo.service;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6.   
  7. public class RemoteService extends Service {  
  8.   
  9.     @Override  
  10.     public IBinder onBind(Intent intent) {  
  11.         MyMessage message = new MyMessage();  
  12.         return message;  
  13.     }  
  14. }  
package com.lovo.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class RemoteService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		MyMessage message = new MyMessage();
		return message;
	}
}

MyMessage类(IMessage的实现类):

  1. package com.lovo.service;  
  2.   
  3. public class MyMessage extends IMessage.Stub {  
  4.     @Override  
  5.     public int getFlag() {  
  6.         return 100;  
  7.     }  
  8.   
  9.     @Override  
  10.     public String getMessage(String msg) {  
  11.         return msg + "Android!";  
  12.     }  
  13. }  
package com.lovo.service;

public class MyMessage extends IMessage.Stub {
	@Override
	public int getFlag() {
		return 100;
	}

	@Override
	public String getMessage(String msg) {
		return msg + "Android!";
	}
}

 

客户端的Activity:

  1. package com.lovo.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. import com.lovo.conn.RemoteServiceConnection;  
  11. import com.lovo.lesson10.R;  
  12.   
  13. public class StartRemoteActivity extends Activity {  
  14.     private Button startServiceBtn;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.remote_main);  
  20.         startServiceBtn = (Button) findViewById(R.id.remote_main_btn_startserver);  
  21.         startServiceBtn.setOnClickListener(new OnClickListener() {  
  22.   
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 Intent intent = new Intent();  
  26.                 intent.setAction("com.lovo.service.REMOTE_SERVICE");  
  27.                 bindService(intent, new RemoteServiceConnection(),  
  28.                         BIND_AUTO_CREATE);  
  29.             }  
  30.         });  
  31.     }  
  32. }  
package com.lovo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.lovo.conn.RemoteServiceConnection;
import com.lovo.lesson10.R;

public class StartRemoteActivity extends Activity {
	private Button startServiceBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.remote_main);
		startServiceBtn = (Button) findViewById(R.id.remote_main_btn_startserver);
		startServiceBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction("com.lovo.service.REMOTE_SERVICE");
				bindService(intent, new RemoteServiceConnection(),
						BIND_AUTO_CREATE);
			}
		});
	}
}


客户端的ServiceConnection的实现类(RemoteServiceConnection):

  1. package com.lovo.conn;  
  2.   
  3. import com.lovo.service.IMessage;  
  4.   
  5. import android.content.ComponentName;  
  6. import android.content.ServiceConnection;  
  7. import android.os.IBinder;  
  8. import android.os.RemoteException;  
  9. import android.util.Log;  
  10.   
  11. public class RemoteServiceConnection implements ServiceConnection {  
  12.   
  13.     @Override  
  14.     public void onServiceConnected(ComponentName name, IBinder service) {  
  15.         // 将远程的代理对象转换成真实对象  
  16.         IMessage message = IMessage.Stub.asInterface(service);  
  17.         try {  
  18.             Log.i("访问远程方法:",  
  19.                     message.getFlag() + " " + message.getMessage("I like "));  
  20.         } catch (RemoteException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.   
  25.     @Override  
  26.     public void onServiceDisconnected(ComponentName name) {  
  27.   
  28.     }  
  29.   
  30. }  
package com.lovo.conn;

import com.lovo.service.IMessage;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class RemoteServiceConnection implements ServiceConnection {

	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		// 将远程的代理对象转换成真实对象
		IMessage message = IMessage.Stub.asInterface(service);
		try {
			Log.i("访问远程方法:",
					message.getFlag() + " " + message.getMessage("I like "));
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {

	}

}

布局文件只有一个按钮在此省略不写。本文只是简单地调用了远程的方法,在实际中可以使用AIDL传递复杂数据。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值