AIDL使用实例(android)

来源:互联网 发布:js 数组转化为字符串 编辑:程序博客网 时间:2024/06/07 03:10
介绍AIDL怎么用,不讲原理(原理网上很多),仅仅记录怎么用AIDL。第一次使用AIDL的人可以借鉴。(以下过程是在eclipse下完成,android4.0 SDK)

AIDL即android 接口描述(定义)语言。

简单说,就是进程和进程之间通信的一种规范,再简单说,就是2个应用之间怎么通信。

所以在这,直接上图

步骤1.首先定义2个android project,名称分别为AIDLServer和AIDLClient。

两个工程的目录结果如下:

截图1
步骤2.两个工程创建好之后,在AIDLClient project和AIDLServer project中都定义一个名为“com.android.my.aidl”的包(当然包名可以自己定义,此包用来放AIDL文件)。

步骤3.创建AIDL文件,并将创建好的文件放到“步骤2”中定义的包中,AIDL文件内容如下:

package com.android.my.aidl; interface ICommonInterface {   int getCount();//此抽象方法将在AIDLServer project中重写,将在AIDLClient project中调用}
注意:以上AIDL文件中package名和“步骤2”中定义的包名一致(因为此文件是放在这个包下面),AIDL中定义了一个java中的接口,但是注意aidl不是java文件,如果是第一次学习AIDL,建议定义简单数据类型,因为aidl的数据类型也有限制(网上有说明)。

步骤4.在AIDLServer project中新定义一个包,在此包下定义一个service,代码如下:

package com.android.server;import com.android.my.aidl.ICommonInterface;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class ServerService extends Service {public class CommonInterface extends ICommonInterface.Stub {                //此方法是AIDL文件中定义的抽象方法的重写,返回一个数字2public int getCount() throws RemoteException {// TODO Auto-generated method stubreturn 2;}}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn new CommonInterface();//注意:此处返回对象就是以上内部类对象}}
service代码写完之后,不要忘了在AndroidManifest.xml中声明,声明代码如下:
        <service android:name="ServerService">            <intent-filter>                <action android:name="com.android.my.aidl.ICommonInterface"/>            </intent-filter>        </service>

到此AIDLServer工程就大功告成了,其中AIDLServerActivity.java是创建工程时自动生成的,没有添加任何代码。

步骤5.接下来是完善AIDLClient的代码。

在AIDLClient工程中就一个AIDLClientActivity类,其中代码如下:

package com.android.client;import com.android.my.aidl.ICommonInterface;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.view.View;import android.widget.Button;public class AIDLClientActivity extends Activity {    /** Called when the activity is first created. */private ICommonInterface common;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button btn = (Button) findViewById(R.id.button1);        btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(ICommonInterface.class.getName());bindService(intent, serviceConn, Context.BIND_AUTO_CREATE);Log.d("YJW", "click");}});    }        private ServiceConnection serviceConn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubLog.d("YJW", "onServiceDisconnecte " );}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubcommon = ICommonInterface.Stub.asInterface(service);try {Log.d("YJW", "count = " + common.getCount());//此处调用的就是AIDLServer中的getCount方法} catch (RemoteException e) {e.printStackTrace();}}};}

注意以上代码就是一个按钮,加上一个监听。以上调用getCount()方法返回的值,我只是以log形式打印出来了(可以再logcat窗口中查看),当然也可以加TextView显示出来

ok,所有代码完毕。

接下来就是把这2个工程安装到手机,然后点开AIDLClient应用,点击按钮,就可以获取AIDLServer返回的值。