Android AIDL的使用
Client(客户端):
1.客户端的aidl必须和服务端的aidl的包名、文件名必须一致。
1 mBinding.btnBinderAidl.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View v) { 4 try { 5 Intent intent = new Intent(); 6 intent.setAction("com.great.android.server.action"); 7 intent.setPackage("com.great.android.server"); 8 bindService(intent, connection, BIND_AUTO_CREATE); 9 } catch (Exception e) { 10 e.printStackTrace(); 11 } 12 } 13 }); 14 15 private IMyAidlInterface myService; 16 public ServiceConnection connection = new ServiceConnection() { 17 @Override 18 public void onServiceConnected(ComponentName name, IBinder service) { 19 myService = IMyAidlInterface.Stub.asInterface(service); 20 try { 21 Toast.makeText(MainActivity.this, myService.getString(), Toast.LENGTH_SHORT).show(); 22 Log.d(TAG, myService.getString()); 23 System.out.println("============================="); 24 25 unbindService(connection); 26 if (myService != null) { 27 myService = null; 28 } 29 } catch (RemoteException e) { 30 e.printStackTrace(); 31 } 32 33 Log.d(TAG, "onServiceConnected:"); 34 } 35 36 @Override 37 public void onServiceDisconnected(ComponentName name) { 38 Log.d(TAG, "onServiceDisconnected:"); 39 myService = null; 40 } 41 }; 42 43 /** 44 * 解绑服务 45 */ 46 @Override 47 protected void onDestroy() { 48 super.onDestroy(); 49 unbindService(connection); 50 if (myService != null) { 51 myService = null; 52 } 53 }
2.在AndroidManifest.xml中添加如下权限:
1 <uses-permission 2 android:name="android.permission.QUERY_ALL_PACKAGES" 3 tools:ignore="QueryAllPackagesPermission" /> 4 5 <queries> 6 <package android:name="com.great.android.server" /> 7 </queries>
Server(服务端):
1 /** 2 * 这个Service是一个独立进程 3 */ 4 public class MyService extends Service { 5 private String TAG = MyService.class.getSimpleName(); 6 7 @Override 8 public IBinder onBind(Intent intent) { 9 Log.d(TAG, "onBind: "); 10 return new MyBind(); 11 } 12 13 @Override 14 public void onCreate() { 15 super.onCreate(); 16 Log.d(TAG, "create: " + Thread.currentThread().getName()); 17 } 18 19 @Override 20 public int onStartCommand(Intent intent, int flags, int startId) { 21 Log.d(TAG, "onStartCommand: "); 22 return super.onStartCommand(intent, flags, startId); 23 } 24 25 @Override 26 public void onDestroy() { 27 super.onDestroy(); 28 Log.d(TAG, "onDestroy: "); 29 } 30 31 public class MyBind extends IMyAidlInterface.Stub { 32 33 @Override 34 public void notifiy() throws RemoteException { 35 try { 36 Thread.sleep(500); 37 } catch (Exception e) { 38 e.printStackTrace(); 39 } 40 Log.d(TAG, "notify sleep: "); 41 } 42 43 @Override 44 public String getString() throws RemoteException { 45 try { 46 Thread.sleep(500); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 Log.d(TAG, "getString: " + Thread.currentThread().getName()); 51 return "hello this is service"; 52 } 53 } 54 }
AndroidManifest.xml权限文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <application android:allowBackup="true" android:icon="@mipmap/logo" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.BaseImproveStudy"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.great.android.server.action" /> </intent-filter> </service> </application> </manifest>
最后:运行服务端,再调用客户端!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人