Aidl测试心得



在使用aidl是需要注意其规则的使用:

一、数据类型:AIDL默认支持一些数据类型,在使用这些数据类型的时候是不需要导包的,但是除了这些类型之外的数据类型,在使用之前必须导包,就算目标文件与当前正在编写的 .aidl 文件在同一个包下——在 Java 中,这种情况是不需要导包的。比如,现在我们编写了两个文件,一个叫做Book.java ,另一个叫做 BookManager.aidl,它们都在 com.lypeer.aidldemo 包下 ,现在我们需要在 .aidl 文件里使用 Book 对象,那么我们就必须在 .aidl 文件里面写上 import com.lypeer.aidldemo.Book; 哪怕 .java 文件和 .aidl 文件就在一个包下。 
默认支持的数据类型包括: 

  • Java中的八种基本数据类型,包括 byte,short,int,long,float,double,boolean,char。
  • String 类型。
  • CharSequence类型。
  • List类型:List中的所有元素必须是AIDL支持的类型之一,或者是一个其他AIDL生成的接口,或者是定义的parcelable(下文关于这个会有详解)。List可以使用泛型。
  • Map类型:Map中的所有元素必须是AIDL支持的类型之一,或者是一个其他AIDL生成的接口,或者是定义的parcelable。Map是不支持泛型的。

二、service与client中的aidl文件包名要相同,否则会出错。

三、在server中的mainfest文件中要在相应的server中增加android:exported="true"和隐式启动的action

四、在执行程序的运转时是:先执行client中的,bindservice方法。2再通过intent隐式启动service中的server。3再执行ServiceConnection中的onServiceConnected的方法,如果有调用aidl中的方法,并在server中执行。直到ServiceConnection接口执行完毕。代码片段如下

package com.example.service;

import com.example.aidlclient.R;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private IRemoteService remoteService;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button) findViewById(R.id.dianwo);
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				buttonClick(v);
			}
		});
    }
     
    ServiceConnection conn = new ServiceConnection() {
         
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
         
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            remoteService = IRemoteService.Stub.asInterface(service);
            try {
            	System.out.println("onServiceConnected");
                int pid = remoteService.getPid();
                int currentPid = Process.myPid();
                System.out.println("currentPID: " + currentPid +"  remotePID: " + pid);
                remoteService.basicTypes(12, 1223, true, 12.2f, 12.3, "我们的爱,我明白");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            System.out.println("bind success! " + remoteService.toString());
        }
    };
         
    /**
     * 监听按钮点击
     * @param view
     */
    public void buttonClick(View view) {
        System.out.println("begin bindService");
        Intent intent = new Intent("duanqing.test.aidl");
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
        System.out.println("begin bindService2");
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.util.Log;

public class DDService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("DDService onCreate........" + "Thread: " + Thread.currentThread().getName());
    }
    @Override
    public IBinder onBind(Intent arg0) {
        System.out.println("DDService onBind");
        return mBinder;
    }
 
    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
        public int getPid(){
            System.out.println("Thread: " + Thread.currentThread().getName());
            System.out.println("DDService getPid ");
            return Process.myPid();
        }
        public void basicTypes(int anInt, long aLong, boolean aBoolean,
            float aFloat, double aDouble, String aString) {
            System.out.println("Thread: " + Thread.currentThread().getName());
            System.out.println("basicTypes aDouble: " + aDouble +" anInt: " + anInt+" aBoolean " + aBoolean+" aString " + aString);
        }
    };
 
}
package com.example.service;
interface IRemoteService{
int getPid();
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}
执行过程如下:(System.out  是打印LOG)
11-14 14:17:26.541 I/System.out( 1049): begin bindService


11-14 14:17:26.650 I/System.out(  758): DDService onCreate........Thread: main


11-14 14:17:26.660 I/System.out(  758): DDService onBind


11-14 14:17:26.680 I/System.out( 1049): begin bindService2


11-14 14:17:26.700 I/System.out( 1049): onServiceConnected


11-14 14:17:26.710 I/Choreographer(  292): Skipped 40 frames!  The application may be doing too much work on its main thread.


11-14 14:17:26.815 I/System.out(  758): Thread: Binder_2


11-14 14:17:26.815 I/System.out(  758): DDService getPid 


11-14 14:17:26.820 I/System.out( 1049): currentPID: 1049  remotePID: 758


11-14 14:17:26.820 I/System.out(  758): Thread: Binder_1


11-14 14:17:26.820 I/System.out(  758): basicTypes aDouble: 12.3 anInt: 12 aBoolean true aString 我们的爱,我明白


11-14 14:17:26.820 I/System.out( 1049): bind success! com.example.service.IRemoteService$Stub$Proxy@40d24398


11-14 14:17:26.830 I/Choreographer( 1049): Skipped 49 frames!  The application may be doing too much work on its main thread.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值