当前位置:   article > 正文

Android 源码编译AIDL 使用实例讲解及Android Studio AIDL的调用详解_系统应用编译 aidl

系统应用编译 aidl
Android日常开发中工作中经常到遇到这种情况,一些不能与源码编译的第三方APP想调用一些framework 层或者platfom 签名应用才有权限调用的一些方法,比如更改系统时间,更改系统字体,写入系统级属性,开启关闭系统设备等,这些方法如果没有底层去开接口支持,第三方应用真是一愁莫展。本文将以一个实例讲解如何以AIDL的方式给上层应用开调用底层方法的接口。

接口需求:

将Android源码frameworks/base/core/java/android/service/persistentdata/PersistentDataBlockManager.java类透出接口给上层调用,关于PersistentDataBlockManager的作用,有兴趣的朋友,可以参考: Android L集成新特性之恢复出厂设置保护之如何实现,类似苹果ID的远程控制功能
先来看AIDL的简介:

AIDL可以跨进程访问其他应用程序,和其他应用程序通讯,说到进程间通讯,很多技术都可以访问,比如广播应用B向A发送指定Action的广播,A就能收到信息,这样也能看成不同应用之间完成了通讯(但是这种通讯是单向的);还如ContentProvider,通过URI接口暴露数据给其他应用访问;但是这种都算不上是应用之间的通讯。

Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.

“只有当你允许来自不同的客户端访问你的服务并且需要处理多线程问题时你才必须使用AIDL”,其他情况下你都可以选择其他方法,如使用Messager,也能跨进程通讯。可见AIDL是处理多线程、多客户端并发访问的。而Messager是单线程处理。

一. 在参与源码编译的应用平台项目中定义AIDL Server

1. 创建一个ADIL 接口文件

在项目中新建包名:com.asus.cnfindphone.service.persistentdata
包下新建文件IDataBlockService.aidl
  1. package com.asus.cnfindphone.service.persistentdata;
  2. // Declare any non-default types here with import statements
  3. interface IDataBlockService {
  4. byte[] read();
  5. int write(in byte[] data);
  6. void wipe();
  7. }

IDataBlockService 接口提供三个接口方法 读 ,写 ,擦除。

2. 创建AIDL Service实现类

包名下创建类文件 DataBlockService.java
目的是要将 PersistentDataBlockManager类透出去给上层应用读写擦
  1. package com.asus.cnfindphone.service.persistentdata;
  2. import android.app.Service;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Binder;
  6. import android.os.IBinder;
  7. import android.os.RemoteException;
  8. import android.util.Log;
  9. import android.service.persistentdata.PersistentDataBlockManager;
  10. /**
  11. * Created by Qinghua_Liu on 2017-2-28.
  12. */
  13. public class DataBlockService extends Service {
  14. private static String TAG = "Qinghua";
  15. @Override
  16. public void onCreate() {
  17. super.onCreate();
  18. }
  19. private boolean bInPSTList(String calling) {
  20. boolean bRes = false;
  21. if (calling.equalsIgnoreCase("asus.findmyphone")) {
  22. bRes = true;
  23. }
  24. Log.d(TAG, "bInPSTList==" + bRes);
  25. return bRes;
  26. }
  27. @Override
  28. public IBinder onBind(Intent intent) {
  29. //String callingApp = intent.getComponent().getPackageName();
  30. return mBind;
  31. }
  32. private final IDataBlockService.Stub mBind = new IDataBlockService.Stub() {
  33. @Override
  34. public byte[] read() throws RemoteException {
  35. byte[] bytes = null;
  36. Log.d(TAG, "DataBlockService.read()");
  37. String callingApp = getApplicationContext().getPackageManager().getNameForUid(Binder.getCallingUid());
  38. Log.d(TAG, "callingApp==" + callingApp);
  39. if (bInPSTList(callingApp)) {
  40. PersistentDataBlockManager pdbManager = (PersistentDataBlockManager) getApplicationContext().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
  41. bytes = pdbManager.read();
  42. //String id ="shenshiid";
  43. //return id.getBytes();
  44. }
  45. Log.d(TAG, "pdbManager.read()==" + new String(bytes));
  46. return bytes;
  47. }
  48. @Override
  49. public int write(byte[] data) throws RemoteException {
  50. Log.d(TAG, "DataBlockService.write()" + new String(data));
  51. String callingApp = getApplicationContext().getPackageManager().getNameForUid(Binder.getCallingUid());
  52. Log.d(TAG, "callingApp==" + callingApp);
  53. if (bInPSTList(callingApp)) {
  54. if (data != null && data.length > 0) {
  55. PersistentDataBlockManager pdbManager = (PersistentDataBlockManager) getApplicationContext().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
  56. pdbManager.write(data);
  57. }
  58. }
  59. return 0;
  60. }
  61. @Override
  62. public void wipe() throws RemoteException {
  63. }
  64. };
  65. }




3.Manifest 静态注册Service


<service android:name=".service.persistentdata.DataBlockService">
<intent-filter>
<action android:name="com.asus.cnfindphone.service.persistentdata.DataBlockService"/>
</intent-filter>
</service>

4. 过滤Service 调用者package:


细心的读者可能 已经发现 2 步骤中的代码:
  1. @Override
  2. public byte[] read() throws RemoteException {
  3. byte[] bytes = null;
  4. Log.d(TAG, "DataBlockService.read()");
  5. String callingApp = getApplicationContext().getPackageManager().getNameForUid(Binder.getCallingUid());
  6. Log.d(TAG, "callingApp==" + callingApp);
  7. if (bInPSTList(callingApp)) {
  8. PersistentDataBlockManager pdbManager = (PersistentDataBlockManager) getApplicationContext().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
  9. bytes = pdbManager.read();
  10. //String id ="shenshiid";
  11. //return id.getBytes();
  12. }
  13. Log.d(TAG, "pdbManager.read()==" + new String(bytes));
  14. return bytes;
  15. }



  1. private boolean bInPSTList(String calling) {
  2. boolean bRes = false;
  3. if (calling.equalsIgnoreCase("asus.findmyphone")) {
  4. bRes = true;
  5. }
  6. Log.d(TAG, "bInPSTList==" + bRes);
  7. return bRes;
  8. }

代码应该很好明白:只允许asus.findmyphone package 调用接口,如果需要让多个包调用可以写入config.xml 定义字串array
需要注意的是获取Service调用者真实包名,防止伪包名:
String callingApp = getApplicationContext().getPackageManager().getNameForUid(Binder.getCallingUid());

5.源码编译AIDL Android.mk的写法:

LOCAL_SRC_FILES := $(call all-java-files-under)

LOCAL_SRC_FILES += \
src/com/asus/cnfindphone/service/persistentdata/IDataBlockService.aidl

6.源码编译安装平台应用。


二. 在Android Studio 中第三方应用调用AIDL接口

上面已经将AIDL Server 部分做完,下面将讲述如果在Android Studio 环境个调试的第三方应用去实现上面AIDL的调用Client

1. Android Studio如何正确导入aidl

Android Studio与Eclipse及源码文件 目录结构不一样,其aidl 文件目录需要单独在app/src/main/ 目录下创建aidl 目录
再在此aidl 目录下创建将要导入的xxx.aidl的包名,最后将xxx.aidl 文件导入,描述的复杂,还是看图吧:
注意这里的IDataBlockService.aidl文件是server 提供的

2.Activity 类中调用接口:

  1. import com.asus.cnfindphone.service.persistentdata.IDataBlockService;
  2. private IDataBlockService dataBlockService;
  3. private ServiceConnection conn;
  4. private void initService(){
  5. conn = new ServiceConnection() {
  6. @Override
  7. public void onServiceDisconnected(ComponentName name) {
  8. Log.d("Qinghua","onServiceDisconnected! ");
  9. }
  10. @Override
  11. public void onServiceConnected(ComponentName name, IBinder service) {
  12. dataBlockService = IDataBlockService.Stub.asInterface((IBinder)service);
  13. Log.d("Qinghua","bind success! ");
  14. }
  15. };
  16. Intent i = new Intent("com.asus.cnfindphone.service.persistentdata.DataBlockService");
  17. i.setPackage("com.asus.cnfindphone");
  18. bindService(i, conn, Context.BIND_AUTO_CREATE);
  19. }
  20. protected void onCreate(Bundle paramBundle) {
  21. super.onCreate(paramBundle);
  22. setContentView(R.layout.activity_main);
  23. initService();
  24. this.location.setOnClickListener(new View.OnClickListener() {
  25. public void onClick(View paramView) {
  26. try {
  27. dataBlockService.write("cissy".getBytes());
  28. }catch (RemoteException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. });
  33. this.test.setOnClickListener(new View.OnClickListener() {
  34. public void onClick(View paramView) {
  35. try {
  36. byte[] bytes = dataBlockService.read();
  37. Log.d("Qinghua","result:"+new String(bytes));
  38. }catch (RemoteException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. });
  43. }
  44. @Override protected void onDestroy()
  45. {
  1. super.onDestroy();
  2. unbindService(conn);
  3. dataBlockService = null;
  4. }


三.调试与Log分析:


Log片断及分析:


#往block 写入数据 cissy
03-01 13:45:00.211 5655-5669/com.asus.cnfindphone D/Qinghua: DataBlockService.write()cissy

#调用者包名过滤
03-01 13:45:00.212 5655-5669/com.asus.cnfindphone D/Qinghua: callingApp==asus.findmyphone
03-01 13:45:00.212 5655-5669/com.asus.cnfindphone D/Qinghua: bInPSTList==true

#调用写入成功,system 级权限允许
03-01 13:45:00.213 3001-3127/system_process E/Qinghua: mAllowedUid==1000callingUid==1000

#读block 数据
03-01 13:45:04.315 5655-5668/com.asus.cnfindphone D/Qinghua: DataBlockService.read()

#调用者包名过滤
03-01 13:45:04.316 5655-5668/com.asus.cnfindphone D/Qinghua: callingApp==asus.findmyphone
03-01 13:45:04.316 5655-5668/com.asus.cnfindphone D/Qinghua: bInPSTList==true

#调用读数据,system 级权限允许
03-01 13:45:04.316 3001-5234/system_process E/Qinghua: mAllowedUid==1000callingUid==1000

#读取之前写的数据成功
03-01 13:45:04.327 5655-5668/com.asus.cnfindphone D/Qinghua: pdbManager.read()==cissy
03-01 13:45:04.327 6488-6488/asus.findmyphone D/Qinghua: result:cissy

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号