详细介绍AIDL 的使用

2024-08-27 20:32
文章标签 使用 介绍 详细 aidl

本文主要是介绍详细介绍AIDL 的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文转自本文为博主许佳佳原创文章,转载请务必注明出处http://blog.csdn.net/double2hao/article/details/51626347   尊重原创

为何要开启多进程?主要有两种情况:

一、一个应用由于自身需要采用多进程模式来实现。比如播放器之类,如果仅仅在service中运行会影响主线程的响应速度,很可能会造成ANR,一般情况下不会这么写;如果仅仅在子线程中运行,一旦开启该线程的Activity被杀死后,线程也被杀死,无法实现后台运行效果,更加不合理。而如果在另外一个进程中使用service后台运行,就显得十分恰当了。

二、由于Android对单个应用所使用的最大内存做了限制,为了加大一个应用可使用的内存,所以通过多进程来获取多份内存空间。


本篇文章demo重点:(demo源码在文章结尾)

1、开启多进程

2、两个进程之间使用AIDL进行通信


开启多进程:

在Android中常用的使用多进程只有一种办法,那就是在AndroidManifest中为四大组件(Activity、Service、Broadcast Receiver、ContentProvier)指定android:process属性。笔者demo中的远程service如下图:



最终绑定该service后在DDMS中进程的显示情况如下图:



可以看到最后的两个进程都是同一个包名,只是第二个是“:remote”。这样就非常简单的开启了多进程。


讲到此处,很多好奇的读者定然有疑问了,“android:process”中的参数到底代表了什么?简单来讲就是代表了新开的这个进程的id。如果两个应用要共享同一个进程就需要用到这个了。

那么笔者此处写的“:remote”又是什么意思呢?“remote”不是关键,这个完全可以自己随意取名字,“:”冒号才是关键

进程名以“:”开头的进程属于当前应用的私有进程,其他应用的组件不可以和它跑在同一个进程中。而进程名不以“:”开头的进程属于全局进程,其他应用可以通过某些方式和它跑在同一个进程中。


两个进程之间使用AIDL进行通信:

笔者此篇文章实现的主要效果:

能够在当前进程中MainActivity,运行另一个进程中开启的Service中实现的方法testMethod(),方法与最终效果如下:




界面上主要有两个按钮,第一个是开启远程进程中的Service,另一个为执行该方法。



主要实现步骤:(主要有三条,分别为AIDL、Service、和调用处(demo中为MainActivity))

1、创建一个AIDL接口,并写入自己要在进程间通信用的抽象方法。

myAIDL.aidl:

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.example.double2.myaidltest;  
  2.   
  3. interface myAIDL {  
  4.   
  5.     void testMethod();  
  6. }  

创建AIDL文件与创建Java文件等类似,直接右击创建即可。android studio中就十分方便,会自动在main文件下创建一个aidl文件夹,并在该文件夹创建于你项目名相同的包名。

可能遇到的小问题:

笔者第一次创建AIDL,在Service中发现找不到该AIDL的包。遇到相同问题的读者可以在创建AIDL并写完抽象方法之后使用build->make project重新构建一下项目。


2、创建一个远程Service,在Service中创建一个类继承AIDL接口中的Stub类并实现Stub中的抽象方法,最后不要忘记在onBind中返回这个类的对象。

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. public class AIDLRemoteService extends Service {  
  2.     private static final String TAG = "AIDLRemoteService";  
  3.   
  4.     private final myAIDL.Stub mBinder=new myAIDL.Stub(){  
  5.         @Override  
  6.         public void testMethod() throws RemoteException {  
  7.             Log.d(TAG, "testMethod: "+"this is myAIDLTest");  
  8.         }  
  9.     };  
  10.   
  11.     @Override  
  12.     public IBinder onBind(Intent intent) {  
  13.         return mBinder;  
  14.     }  
  15. }  


3、在要调用的地方(笔者demo中就为MainActivity中)绑定该Service,将Service返回的Binder对象转换成AIDL接口所属的类型,接着直接调用AIDL的方法。

在成功连接之后,将Service返回的Binder对象转换成AIDL接口所属的类型:

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. private myAIDL mMyAIDL;  
  2.     private ServiceConnection mServiceConnection = new ServiceConnection() {  
  3.         @Override  
  4.         public void onServiceConnected(ComponentName name, IBinder service) {  
  5.             Log.e(TAG, "onServiceConnected");  
  6.             mMyAIDL = myAIDL.Stub.asInterface(service);  
  7.         }  
  8.   
  9.         @Override  
  10.         public void onServiceDisconnected(ComponentName name) {  
  11.             Log.e(TAG, "onServiceDisconnected");  
  12.             mMyAIDL = null;  
  13.         }  
  14.     };  

在调用处直接使用:

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. btnStartMethod.setOnClickListener(new View.OnClickListener() {  
  2.             @Override  
  3.             public void onClick(View v) {  
  4.   
  5.                 try {  
  6.                     mMyAIDL.testMethod();  
  7.                 } catch (RemoteException e) {  
  8.                     Toast.makeText(MainActivity.this"服务被异常杀死,请重新开启。", Toast.LENGTH_SHORT).show();  
  9.                 }  
  10.   
  11.             }  
  12.         });  


demo项目结构:



myAIDL.aidl:

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.example.double2.myaidltest;  
  2.   
  3. interface myAIDL {  
  4.   
  5.     void testMethod();  
  6. }  


AIDLRemoteService:

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.example.double2.myaidltest;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6. import android.os.RemoteException;  
  7. import android.util.Log;  
  8.   
  9. /** 
  10.  * 项目名称:MyAIDLTest 
  11.  * 创建人:Double2号 
  12.  * 创建时间:2016/6/10 8:13 
  13.  * 修改备注: 
  14.  */  
  15. public class AIDLRemoteService extends Service {  
  16.     private static final String TAG = "AIDLRemoteService";  
  17.   
  18.     private final myAIDL.Stub mBinder=new myAIDL.Stub(){  
  19.         @Override  
  20.         public void testMethod() throws RemoteException {  
  21.             Log.d(TAG, "testMethod: "+"this is myAIDLTest");  
  22.         }  
  23.     };  
  24.   
  25.     @Override  
  26.     public IBinder onBind(Intent intent) {  
  27.         return mBinder;  
  28.     }  
  29. }  


MainActivity:

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.example.double2.myaidltest;  
  2.   
  3. import android.content.ComponentName;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.ServiceConnection;  
  7. import android.os.Bundle;  
  8. import android.os.IBinder;  
  9. import android.os.RemoteException;  
  10. import android.support.v7.app.AppCompatActivity;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends AppCompatActivity {  
  17.   
  18.     private static final String TAG = "MainActivity";  
  19.     private Button btnBindService;  
  20.     private Button btnStartMethod;  
  21.   
  22.     private myAIDL mMyAIDL;  
  23.     private ServiceConnection mServiceConnection = new ServiceConnection() {  
  24.         @Override  
  25.         public void onServiceConnected(ComponentName name, IBinder service) {  
  26.             Log.e(TAG, "onServiceConnected");  
  27.             mMyAIDL = myAIDL.Stub.asInterface(service);  
  28.         }  
  29.   
  30.         @Override  
  31.         public void onServiceDisconnected(ComponentName name) {  
  32.             Log.e(TAG, "onServiceDisconnected");  
  33.             mMyAIDL = null;  
  34.         }  
  35.     };  
  36.   
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.activity_main);  
  41.   
  42.         initView();  
  43.     }  
  44.   
  45.     private void initView() {  
  46.         btnBindService = (Button) findViewById(R.id.btn_bind_service);  
  47.         btnStartMethod = (Button) findViewById(R.id.btn_start_method);  
  48.         btnBindService.setOnClickListener(new View.OnClickListener() {  
  49.             @Override  
  50.             public void onClick(View v) {  
  51.                 Intent intent = new Intent(MainActivity.this, AIDLRemoteService.class);  
  52.                 bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);  
  53.             }  
  54.         });  
  55.   
  56.         btnStartMethod.setOnClickListener(new View.OnClickListener() {  
  57.             @Override  
  58.             public void onClick(View v) {  
  59.   
  60.                 try {  
  61.                     mMyAIDL.testMethod();  
  62.                 } catch (RemoteException e) {  
  63.                     Toast.makeText(MainActivity.this"服务被异常杀死,请重新开启。", Toast.LENGTH_SHORT).show();  
  64.                 }  
  65.   
  66.             }  
  67.         });  
  68.     }  
  69.   
  70.     @Override  
  71.     protected void onDestroy() {  
  72.         super.onDestroy();  
  73.         unbindService(mServiceConnection);  
  74.     }  
  75. }  

activity_main:

[html]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     >  
  8.   
  9.     <Button  
  10.         android:id="@+id/btn_bind_service"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:textSize="20sp"  
  14.         android:text="bindService"/>  
  15.   
  16.     <Button  
  17.         android:id="@+id/btn_start_method"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:textSize="20sp"  
  21.         android:text="startMethod"/>  
  22.   
  23. </LinearLayout>  

AndroidManifest:

[html]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.           package="com.example.double2.myaidltest">  
  4.   
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:supportsRtl="true"  
  10.         android:theme="@style/AppTheme">  
  11.         <activity android:name=".MainActivity">  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN"/>  
  14.   
  15.                 <category android:name="android.intent.category.LAUNCHER"/>  
  16.             </intent-filter>  
  17.         </activity>  
  18.   
  19.         <service android:name=".AIDLRemoteService"  
  20.             android:process=":remote"/>  
  21.     </application>  
  22.   
  23. </manifest>  


demo源码地址:http://download.csdn.net/detail/double2hao/9545551

这篇关于详细介绍AIDL 的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1112704

相关文章

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF