Android的Aidl安装方法

AIDL(AndRoid 接口描述语言)是一种借口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象转化成AIDL可识别的参数(可能是多个参数), 然后使用AIDL来传递这些参数, 在消息的接收端, 使用这些参数组装成自己需要的对象. 

AIDL的IPC的机制和COM或CORBA类似, 是基于接口的,但它是轻量级的。它使用代理类在客户端和实现层间传递值. 如果要使用AIDL, 需要完成2件事情: 1. 引入AIDL的相关类.; 2. 调用aidl产生的class. 
我通过学习最基本方法写了一个小类。

package com.smart.aidl;

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

public class SmartService extends Service {

	public class MyServiceImpl extends AidlService.Stub {

		@Override
		public String getValue() throws RemoteException {
			// TODO Auto-generated method stub
			return "Smart like Android!";
		}

	}

	@Override
	public IBinder onBind(Intent arg0) {

		return new MyServiceImpl();
	}

}

package com.smart.aidl;

import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
package com.smart.aidl;
interface AidlService
{
    String getValue();
}

posted on 2011-03-08 10:21  llb988  阅读(375)  评论(0编辑  收藏  举报

导航