安卓中AIDL的使用方法快速入门
1.AIDL是什么?
AIDL全稱是Android Interface Definition Language,即安卓接口定義語(yǔ)言。
2.AIDL是用來(lái)做什么的?(為什么要有AIDL)
AIDL是用來(lái)進(jìn)行進(jìn)程間通信(IPC全稱interprocess communication?)的。
3.如何使用AIDL?
對(duì)于AIDL的使用,
服務(wù)端需要完成的任務(wù)是:
?
①.寫一個(gè)xxxx.aidl文件
②.寫一個(gè)Service并在AndroidManifest.xml中聲明它。(注意:這個(gè)service里面有一個(gè)引用了實(shí)現(xiàn)xxxx.Stub抽象類的IBinder對(duì)象,這個(gè)對(duì)象將在service的onBind方法里面返回給調(diào)用者)
客戶端的任務(wù):
①.使用和服務(wù)端相同的那個(gè)aidl文件
②.在實(shí)現(xiàn)了ServiceConnection接口的onServiceConnected(ComponentName name, IBinder service)方法中調(diào)用myvar =?testidl.Stub.asInterface(service)保存得到的對(duì)象,其中myvar是xxxx的類型
這么說(shuō)還是不夠清楚,下面直接上代碼。
首先是服務(wù)端的
//testidl.idl文件的內(nèi)容 package com.example.xxnote;interface testidl {void TestFunction(int anInt, long aLong, boolean aBoolean, float aFloat,double aDouble, String aString); }//myaidlservice.java文件的內(nèi)容 package com.example.xxnote;import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException;public class myaidlservice extends Service {private final IBinder myStub = new testidl.Stub() {@Overridepublic void TestFunction(int anInt, long aLong, boolean aBoolean,float aFloat, double aDouble, String aString)throws RemoteException {// TODO Auto-generated method stubSystem.out.println("basicTypes()");System.err.println("Service"+anInt + "," + aLong + "," + aBoolean + ","+ aFloat + "," + aDouble + "," + aString);}};@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("AIDL Service onBind, and return IBinder");return myStub;}@Overridepublic void onCreate() {// TODO Auto-generated method stubSystem.out.println("AIDL Service onCreate");super.onCreate();}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("AIDL Service onUnbind");return super.onUnbind(intent);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubSystem.out.println("AIDL Service onDestroy");super.onDestroy();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem.out.println("AIDL Service onStartCommand");return super.onStartCommand(intent, flags, startId);} }
1 <!-- AndroidManifest.xml 的 application 標(biāo)簽的內(nèi)容--> 2 <service android:name="myaidlservice"> 3 <intent-filter > 4 <action android:name="zhenshi.mafan.qisia.aidl"/> 5 </intent-filter> 6 </service>
?對(duì)于客戶端,首先需要把a(bǔ)idl文件復(fù)制到相應(yīng)的目錄本例中是src/com/example/xxnote/testidl.aidl
package com.example.xxnote.callaidl;import com.example.xxnote.testidl;import android.R.bool; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends Activity {private testidl mytTestidl;private ServiceConnection connection;private boolean isServiceConnected;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);connection = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubSystem.out.println("Client onServiceDisconnected");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubSystem.out.println("Client onServiceConnected");mytTestidl = testidl.Stub.asInterface(service);isServiceConnected = true;}};}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}//關(guān)閉服務(wù)按鈕的事件public void StopMyService(View v) {System.out.println("StopMyService");isServiceConnected = false;unbindService(connection);mytTestidl = null;}//開(kāi)啟服務(wù)按鈕的事件public void StartMyService(View v) {System.out.println("before bindService()");bindService(new Intent().setAction("zhenshi.mafan.qisia.aidl"), connection,Context.BIND_AUTO_CREATE);/*** bindService是異步的所以執(zhí)行bindService方法的同時(shí)也開(kāi)始執(zhí)行下面的方法了,* Debug跟蹤了一下程序發(fā)現(xiàn)貌似Activity里面所有的方法都是在主線程的loop()方法* 循環(huán)里面以消息隊(duì)列里面的一個(gè)消息的樣子執(zhí)行的,也就是此處的StartMyService方* 法對(duì)應(yīng)的消息處理完(此函數(shù)返回)后,才能處理下一個(gè)消息,即執(zhí)行onServiceConnected回調(diào)方法* * 試驗(yàn)了一下,StopMyService里面把mytTestidl賦值為null,即每次解除服務(wù)綁定后都重置mytTestidl為null* 果然每次下面的語(yǔ)句:* mytTestidl.basicTypes(1, 1, true, 100.0f, 200.0, "ssss");* 都報(bào)空指針異常*/try {mytTestidl.TestFunction(1, 1, true, 100.0f, 200.0, "ssss");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}} }
這里還有一個(gè)需要注意的地方,就是bindService方法?的時(shí)候用到的intent,通過(guò)setAction可以成功啟動(dòng)服務(wù),用setClassName就不能,不知道什么原因,暫時(shí)留待以后解決。
找到原因了,用setClassName的時(shí)候必須使用全限定類名,如:new Intent().setClassName("com.example.client.callaidl", "com.example.client.callaidl.testact")
?
轉(zhuǎn)載于:https://www.cnblogs.com/xxNote/p/5496156.html
總結(jié)
以上是生活随笔為你收集整理的安卓中AIDL的使用方法快速入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ActiveMQ(一)简介与架构
- 下一篇: 使用RMAN验证备份的有效性