日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > ChatGpt >内容正文

ChatGpt

安卓中AIDL的使用方法快速入门

發布時間:2025/3/18 ChatGpt 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓中AIDL的使用方法快速入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.AIDL是什么?

AIDL全稱是Android Interface Definition Language,即安卓接口定義語言。

2.AIDL是用來做什么的?(為什么要有AIDL)

AIDL是用來進行進程間通信(IPC全稱interprocess communication?)的。

3.如何使用AIDL?

對于AIDL的使用,

服務端需要完成的任務是:

?

①.寫一個xxxx.aidl文件

②.寫一個Service并在AndroidManifest.xml中聲明它。(注意:這個service里面有一個引用了實現xxxx.Stub抽象類的IBinder對象,這個對象將在service的onBind方法里面返回給調用者)

客戶端的任務:

①.使用和服務端相同的那個aidl文件

②.在實現了ServiceConnection接口的onServiceConnected(ComponentName name, IBinder service)方法中調用myvar =?testidl.Stub.asInterface(service)保存得到的對象,其中myvar是xxxx的類型

這么說還是不夠清楚,下面直接上代碼。

首先是服務端的

//testidl.idl文件的內容 package com.example.xxnote;interface testidl {void TestFunction(int anInt, long aLong, boolean aBoolean, float aFloat,double aDouble, String aString); }

 

//myaidlservice.java文件的內容 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 標簽的內容--> 2 <service android:name="myaidlservice"> 3 <intent-filter > 4 <action android:name="zhenshi.mafan.qisia.aidl"/> 5 </intent-filter> 6 </service>

?對于客戶端,首先需要把aidl文件復制到相應的目錄本例中是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);}//關閉服務按鈕的事件public void StopMyService(View v) {System.out.println("StopMyService");isServiceConnected = false;unbindService(connection);mytTestidl = null;}//開啟服務按鈕的事件public void StartMyService(View v) {System.out.println("before bindService()");bindService(new Intent().setAction("zhenshi.mafan.qisia.aidl"), connection,Context.BIND_AUTO_CREATE);/*** bindService是異步的所以執行bindService方法的同時也開始執行下面的方法了,* Debug跟蹤了一下程序發現貌似Activity里面所有的方法都是在主線程的loop()方法* 循環里面以消息隊列里面的一個消息的樣子執行的,也就是此處的StartMyService方* 法對應的消息處理完(此函數返回)后,才能處理下一個消息,即執行onServiceConnected回調方法* * 試驗了一下,StopMyService里面把mytTestidl賦值為null,即每次解除服務綁定后都重置mytTestidl為null* 果然每次下面的語句:* mytTestidl.basicTypes(1, 1, true, 100.0f, 200.0, "ssss");* 都報空指針異常*/try {mytTestidl.TestFunction(1, 1, true, 100.0f, 200.0, "ssss");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}} }

  

這里還有一個需要注意的地方,就是bindService方法?的時候用到的intent,通過setAction可以成功啟動服務,用setClassName就不能,不知道什么原因,暫時留待以后解決。

找到原因了,用setClassName的時候必須使用全限定類名,如:new Intent().setClassName("com.example.client.callaidl", "com.example.client.callaidl.testact")

 

?

轉載于:https://www.cnblogs.com/xxNote/p/5496156.html

總結

以上是生活随笔為你收集整理的安卓中AIDL的使用方法快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。