【Binder 机制】AIDL 分析 ( 创建 Service 服务 | 绑定 Service 远程服务 )
生活随笔
收集整理的這篇文章主要介紹了
【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 對(duì)象 , 從系統(tǒng)中獲取對(duì)應(yīng)的遠(yuǎn)程服務(wù)或代理對(duì)象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 對(duì)象 , 從系統(tǒng)中獲取對(duì)應(yīng)的遠(yuǎn)程服務(wù)或代理對(duì)象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 對(duì)象 , 點(diǎn)擊獲取按鈕 , 即可在日志中打印之前添加的所有 Student 對(duì)象 ;
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 远程服务 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Binder 机制】AIDL 分析 (
- 下一篇: 【Binder 机制】AIDL 分析 (