日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Android中AIDL的簡(jiǎn)單使用

AIDL這里就不加累述它的概念定義等等,免得長(zhǎng)篇大幅。下面介紹的是簡(jiǎn)單的一次使用AIDL成功與service通信的一個(gè)例子:

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的惠山網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

1.在項(xiàng)目包下新建一個(gè)IInfo.aidl,并在其中添加你要調(diào)用的方法,格式和java中接口一樣。package com.android.server;

 
 
  1. interface IInfo {  
  2.     boolean start(); 
  3.     void stop(); 
  4.     void locate(int x, int y); 
  5.     void move(int dx, int dy); 
  6.     void getLocation(inout int[] p);//參數(shù)為數(shù)組的話,可以加上inout,不然會(huì)報(bào)錯(cuò) 
  7.     void setTimeout(int t); 
  8.     int getTimeout(); 
  9.     void setBitmap(inout byte[] bmp, int width, int height);}   

正確寫好之后,eclipse的adt會(huì)自動(dòng)在gen目錄下生成一個(gè)IInfo.java文件

2.新建一個(gè)CursorService.java類,繼承IInfo.stub,如下:

 
 
  1. package com.android.server; 
  2. public class CursorService extends ICursorInfo.Stub{ 
  3.     final boolean hasService; 
  4.     public CursorService() { 
  5.         hasService = initializeJNI(); 
  6.     } 
  7.     public synchronized boolean start() { 
  8.         if (hasService) 
  9.             return start0(); 
  10.         return false; 
  11.     } 
  12.     public synchronized void stop() { 
  13.         if (hasService) 
  14.             stop0(); 
  15.     } 
  16.     public synchronized void locate(int x, int y) { 
  17.         if (hasService) 
  18.             locate0(x, y); 
  19.     } 
  20.   
  21.     public synchronized void move(int dx, int dy) { 
  22.        if (hasService) 
  23.            move0(dx, dy); 
  24.     } 
  25.   
  26.     public synchronized void getLocation(int[] p) { 
  27.         if (p == null) 
  28.             throw new NullPointerException("p is null"); 
  29.         if (p.length < 2) 
  30.             throw new IllegalArgumentException("p.len must >= 2"); 
  31.         if (hasService) 
  32.             getPosition0(p); 
  33.     } 
  34.     public synchronized void setTimeout(int t) { 
  35.         if (hasService) 
  36.             setTimeout0(t); 
  37.     } 
  38.   
  39.     public synchronized int getTimeout() { 
  40.         if (hasService) 
  41.             return getTimeout0(); 
  42.         return -1; 
  43.     } 
  44.   
  45.     public void setBitmap(byte[] bmp, int width, int height) { 
  46.         if(bmp == null) 
  47.             throw new NullPointerException("bmp is null"); 
  48.         if(width < 0 || height < 0) 
  49.            throw new IllegalArgumentException("width < 0 || height < 0"); 
  50.        if(width * height > bmp.length) 
  51.             throw new IndexOutOfBoundsException("bmp less than width*height"); 
  52.         setBitmap0(bmp,width,height); 
  53.     } 

在其中實(shí)現(xiàn)你aAIDL中的方法

3. 新建一個(gè)Manager類,在其中構(gòu)造一個(gè)內(nèi)部服務(wù)連接類,實(shí)現(xiàn)ServiceConnection接口:

 
 
  1. public class Manager { 
  2.     private static final String TAG = "Manager"; 
  3.     private IInfo   iCurSer; 
  4.     private Manager(){ 
  5.     } 
  6.       
  7.     public Manager(Context ctx){ 
  8.         this.context = ctx; 
  9.         new Manager(); 
  10.     } 
  11.       
  12.        /**這里就可以與service正常通信,調(diào)用service中的方法**/ 
  13.     public void startService(){ 
  14.         Intent intent=new Intent("com.android.server.CursorService"); 
  15.         context.bindService(intent,new CursorServiceConnection(), 
  16.                 Service.BIND_AUTO_CREATE); 
  17.     } 
  18.     /** 
  19.      * 實(shí)現(xiàn)ServiceConnection接口 
  20.      * */ 
  21.     public final class CursorServiceConnection implements ServiceConnection{ 
  22.        // 和CursorService綁定時(shí)系統(tǒng)回調(diào)這個(gè)方法 
  23.         @Override 
  24.         public void onServiceConnected(ComponentName name, IBinder service) { 
  25.            // 此處不能使用強(qiáng)制轉(zhuǎn)換, 應(yīng)該調(diào)用Stub類的靜態(tài)方法獲得CursorService接口的實(shí)例對(duì)象 
  26.            iCurSer=ICursorInfo.Stub.asInterface(service); 
  27.         } 
  28.   
  29.         //解除和CursorService的綁定時(shí)系統(tǒng)回調(diào)這個(gè)方法 
  30.         @Override 
  31.         public void onServiceDisconnected(ComponentName name) { 
  32.             iCurSer=null; 
  33.         } 
  34.     } 

Android中AIDL的簡(jiǎn)單使用就mark到這吧,希望對(duì)剛剛學(xué)Android的開發(fā)者能有些幫助。


網(wǎng)站題目:Android中AIDL的簡(jiǎn)單使用
標(biāo)題路徑:http://m.5511xx.com/article/cdiigdg.html