Android AIDL的入門使用教程

來源:互聯網
上載者:User

最近博主在做一個項目要求用到AIDL調用,於是研究了半天終於弄明白了,在這裡給入門的同學說一下:

首先,在eclipse下建立兩個工程 工程1 和 工程2 我們把工程1作為被調用的工程,工程2作為主調工程.是項1的樹形圖。包裡有一個AIDL檔案。。這就是介面檔案,在裡面定義了介面。然後通過Android的Service實現了裡面的介面。MyAIDL.aidl檔案內容如下,文法跟java類似,裡面只定義了一個介面
interface MyAIDL{    void print(in String str);}


MyAILDService.java檔案內容如下:注意觀察是怎麼跟aidl檔案建立聯絡的。另一個activity檔案不用管他。。

package uestc.muhuan;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class MyAIDLService extends Service {private MyAIDL.Stub mBind = new MyAIDL.Stub() {@Overridepublic void print(String str) throws RemoteException {// TODO Auto-generated method stubSystem.out.println(str);     }   };@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn mBind;   }}

最後比比較重要的一點就是在項目1的Manifest檔案中要註冊你的service,同時要為你的sevice加上一個action標記。如下所示:

     <service            android:name=".MyAIDLService"        android:process=":remote"       //這一行網上說遠程調用必須得有,但是本人測試發現                                  //有可無,只是在有這一句的時候,系統會多出一個remote進程,但是不影響                            // 功能,水平有限,不知何故。            >                          <intent-filter >                <action android:name="uestc.muhuan.service_action" >                </action>            </intent-filter>        </service>

到此為止,項目1完工了,把這個工程燒到你的手機,或者模擬器上。

下面這部分是項目2:
是項目2的樹形圖,注意看在項目2裡有一個跟項目1一樣的包,包裡同樣有一個AIDL檔案,這個檔案跟項目1 的檔案是一模一樣的 直接拖過來就好了。。但是注意這裡面沒有Service實現檔案。。

MyAIDLTest2Activity.java 的代碼如下

package uestc.muhua;import uestc.muhuan.MyAIDL;import uestc.muhua.R;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;public class MyAIDLTest2Activity extends Activity {private MyAIDL myAIDL = null;ServiceConnection sConnection = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub   }@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubmyAIDL = MyAIDL.Stub.asInterface(service);  try {myAIDL.print("nihao2");  } catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();      }    }  };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);System.out.println("2qidong");setContentView(R.layout.main);init();  }private void init() {this.bindService(new Intent("uestc.muhuan.service_action"),sConnection, BIND_AUTO_CREATE);  }@Overrideprotected void onDestroy() {// TODO Auto-generated method stubthis.unbindService(sConnection);super.onDestroy();   }}

其實很簡單,就是通過ServiceConnect綁定服務,然後得到執行個體,調用介面。。。一氣呵成。呵呵。看來上面的代碼是不是很簡單啊。。歡迎大家交流。。

http://blog.sina.com.cn/s/blog_9d708b4f01016w2w.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.