详细介绍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打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图