新聞中心
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Android中AIDL如何使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站制作、成都做網(wǎng)站與策劃設(shè)計,鄲城網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:鄲城等地區(qū)。鄲城做網(wǎng)站價格咨詢:13518219792
1.在項(xiàng)目包下新建一個IInfo.aidl,并在其中添加你要調(diào)用的方法,格式和java中接口一樣。package com.android.server;
interface IInfo { boolean start(); void stop(); void locate(int x, int y); void move(int dx, int dy); void getLocation(inout int[] p);//參數(shù)為數(shù)組的話,可以加上inout,不然會報錯 void setTimeout(int t); int getTimeout(); void setBitmap(inout byte[] bmp, int width, int height);}
正確寫好之后,eclipse的adt會自動在gen目錄下生成一個IInfo.java文件
2.新建一個CursorService.java類,繼承IInfo.stub,如下:
package com.android.server; public class CursorService extends ICursorInfo.Stub{ final boolean hasService; public CursorService() { hasService = initializeJNI(); } public synchronized boolean start() { if (hasService) return start0(); return false; } public synchronized void stop() { if (hasService) stop0(); } public synchronized void locate(int x, int y) { if (hasService) locate0(x, y); } public synchronized void move(int dx, int dy) { if (hasService) move0(dx, dy); } public synchronized void getLocation(int[] p) { if (p == null) throw new NullPointerException("p is null"); if (p.length < 2) throw new IllegalArgumentException("p.len must >= 2"); if (hasService) getPosition0(p); } public synchronized void setTimeout(int t) { if (hasService) setTimeout0(t); } public synchronized int getTimeout() { if (hasService) return getTimeout0(); return -1; } public void setBitmap(byte[] bmp, int width, int height) { if(bmp == null) throw new NullPointerException("bmp is null"); if(width < 0 || height < 0) throw new IllegalArgumentException("width < 0 || height < 0"); if(width * height > bmp.length) throw new IndexOutOfBoundsException("bmp less than width*height"); setBitmap0(bmp,width,height); }
在其中實(shí)現(xiàn)你aAIDL中的方法
3. 新建一個Manager類,在其中構(gòu)造一個內(nèi)部服務(wù)連接類,實(shí)現(xiàn)ServiceConnection接口:
public class Manager { private static final String TAG = "Manager"; private IInfo iCurSer; private Manager(){ } public Manager(Context ctx){ this.context = ctx; new Manager(); } /**這里就可以與service正常通信,調(diào)用service中的方法**/ public void startService(){ Intent intent=new Intent("com.android.server.CursorService"); context.bindService(intent,new CursorServiceConnection(), Service.BIND_AUTO_CREATE); } /** * 實(shí)現(xiàn)ServiceConnection接口 * */ public final class CursorServiceConnection implements ServiceConnection{ // 和CursorService綁定時系統(tǒng)回調(diào)這個方法 @Override public void onServiceConnected(ComponentName name, IBinder service) { // 此處不能使用強(qiáng)制轉(zhuǎn)換, 應(yīng)該調(diào)用Stub類的靜態(tài)方法獲得CursorService接口的實(shí)例對象 iCurSer=ICursorInfo.Stub.asInterface(service); } //解除和CursorService的綁定時系統(tǒng)回調(diào)這個方法 @Override public void onServiceDisconnected(ComponentName name) { iCurSer=null; } } }
上述就是小編為大家分享的Android中AIDL如何使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標(biāo)題:Android中AIDL如何使用
本文來源:http://www.dlmjj.cn/article/pjpepi.html