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.




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章