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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人工智能 > ChatGpt >内容正文

ChatGpt

【Binder 机制】AIDL 分析 ( 创建 Service 服务 | 绑定 Service 远程服务 )

發(fā)布時(shí)間:2025/6/17 ChatGpt 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Binder 机制】AIDL 分析 ( 创建 Service 服务 | 绑定 Service 远程服务 ) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 一、創(chuàng)建 Service 遠(yuǎn)程服務(wù)
    • 1、創(chuàng)建 Service
    • 2、AndroidManifest.xml 清單文件中配置 Service
  • 二、綁定 Service 遠(yuǎn)程服務(wù)
    • 1、核心代碼
    • 2、完整代碼
    • 3、運(yùn)行結(jié)果





一、創(chuàng)建 Service 遠(yuǎn)程服務(wù)




1、創(chuàng)建 Service


package kim.hsl.aidl_demo;import androidx.appcompat.app.AppCompatActivity;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.util.Log; import android.view.View; import android.widget.Toast;import java.util.List;public class MainActivity extends AppCompatActivity {public static final String TAG = "MainActivity";private IMyAidlInterface aidl;private ServiceConnection serviceConnection = new ServiceConnection() {/*** 傳入需要的 Service , 讓系統(tǒng)尋找指定的遠(yuǎn)程服務(wù)* @param name* @param service*/@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// 通過 IBinder 對象 , 從系統(tǒng)中獲取對應(yīng)的遠(yuǎn)程服務(wù)或代理對象aidl = IMyAidlInterface.Stub.asInterface(service);Log.i(TAG, "AIDL 獲取成功");}@Overridepublic void onServiceDisconnected(ComponentName name) {aidl = null;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 通過 Action 和 包名 , 綁定遠(yuǎn)程服務(wù)Intent intent = new Intent("android.intent.action.MyService");intent.setPackage("kim.hsl.aidl_demo");bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);findViewById(R.id.add).setOnClickListener((View view)->{try {aidl.addStudent(new Student("Tom"));} catch (RemoteException e) {e.printStackTrace();}});findViewById(R.id.get).setOnClickListener((View view)->{try {List<Student> students = aidl.getStudents();Log.i(TAG, "students = " + students);} catch (RemoteException e) {e.printStackTrace();}});} }

2、AndroidManifest.xml 清單文件中配置 Service


<serviceandroid:name=".MyService"android:enabled="true"android:exported="true"><intent-filter><action android:name="android.intent.action.MyService" /></intent-filter></service>



二、綁定 Service 遠(yuǎn)程服務(wù)




1、核心代碼


通過 Action 和 包名 , 綁定遠(yuǎn)程服務(wù) , 其中 Action 是在 AndroidManifest.xml 清單文件中配置的 ;

// 通過 Action 和 包名 , 綁定遠(yuǎn)程服務(wù)Intent intent = new Intent("android.intent.action.MyService");intent.setPackage("kim.hsl.aidl_demo");bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

2、完整代碼


完整代碼如下 :

package kim.hsl.aidl_demo;import androidx.appcompat.app.AppCompatActivity;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.util.Log; import android.view.View; import android.widget.Toast;import java.util.List;public class MainActivity extends AppCompatActivity {public static final String TAG = "MainActivity";private IMyAidlInterface aidl;private ServiceConnection serviceConnection = new ServiceConnection() {/*** 傳入需要的 Service , 讓系統(tǒng)尋找指定的遠(yuǎn)程服務(wù)* @param name* @param service*/@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// 通過 IBinder 對象 , 從系統(tǒng)中獲取對應(yīng)的遠(yuǎn)程服務(wù)或代理對象aidl = IMyAidlInterface.Stub.asInterface(service);Log.i(TAG, "AIDL 獲取成功");}@Overridepublic void onServiceDisconnected(ComponentName name) {aidl = null;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 通過 Action 和 包名 , 綁定遠(yuǎn)程服務(wù)Intent intent = new Intent("android.intent.action.MyService");intent.setPackage("kim.hsl.aidl_demo");bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);findViewById(R.id.add).setOnClickListener((View view)->{try {aidl.addStudent(new Student("Tom"));} catch (RemoteException e) {e.printStackTrace();}});findViewById(R.id.get).setOnClickListener((View view)->{try {List<Student> students = aidl.getStudents();Log.i(TAG, "students = " + students);} catch (RemoteException e) {e.printStackTrace();}});} }

3、運(yùn)行結(jié)果


點(diǎn)擊添加按鈕 , 即可向遠(yuǎn)程服務(wù)中添加 Student 對象 , 點(diǎn)擊獲取按鈕 , 即可在日志中打印之前添加的所有 Student 對象 ;

2021-09-16 15:11:14.492 27781-27781/kim.hsl.aidl_demo I/MainActivity: AIDL 獲取成功 2021-09-16 15:11:14.499 27781-27866/kim.hsl.aidl_demo I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 1 2021-09-16 15:11:14.499 27781-27866/kim.hsl.aidl_demo I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0 2021-09-16 15:11:14.499 27781-27866/kim.hsl.aidl_demo I/OpenGLRenderer: Initialized EGL, version 1.4 2021-09-16 15:11:27.704 27781-27781/kim.hsl.aidl_demo I/MainActivity: students = [name=Tom, name=Tom]2021-09-16 15:11:40.729 27781-27781/kim.hsl.aidl_demo I/MainActivity: students = [name=Tom, name=Tom, name=Tom, name=Tom, name=Tom, name=Tom, name=Tom, name=Tom] 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的【Binder 机制】AIDL 分析 ( 创建 Service 服务 | 绑定 Service 远程服务 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。