av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

Android中AIDL的簡單使用

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

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

1.在項目包下新建一個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,不然會報錯 
  7.     void setTimeout(int t); 
  8.     int getTimeout(); 
  9.     void setBitmap(inout byte[] bmp, int width, int height);}   

正確寫好之后,eclipse的adt會自動在gen目錄下生成一個IInfo.java文件

2.新建一個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.     } 

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

3. 新建一個Manager類,在其中構(gòu)造一個內(nèi)部服務連接類,實現(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.      * 實現(xiàn)ServiceConnection接口 
  20.      * */ 
  21.     public final class CursorServiceConnection implements ServiceConnection{ 
  22.        // 和CursorService綁定時系統(tǒng)回調(diào)這個方法 
  23.         @Override 
  24.         public void onServiceConnected(ComponentName name, IBinder service) { 
  25.            // 此處不能使用強制轉(zhuǎn)換, 應該調(diào)用Stub類的靜態(tài)方法獲得CursorService接口的實例對象 
  26.            iCurSer=ICursorInfo.Stub.asInterface(service); 
  27.         } 
  28.   
  29.         //解除和CursorService的綁定時系統(tǒng)回調(diào)這個方法 
  30.         @Override 
  31.         public void onServiceDisconnected(ComponentName name) { 
  32.             iCurSer=null; 
  33.         } 
  34.     } 

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


新聞名稱:Android中AIDL的簡單使用
URL鏈接:http://uogjgqi.cn/article/cdiigdg.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流