Android Shell 中有 service 命令,其二进制文件位于 /system/bin/service,该命令可以用来调试,甚至是应用于开发。
用法
先看一下 service 用法:
1 | adb shell service |
service/service -h/service -?都是用来获取service命令帮助信息的命令。service list用来列出当前可用的系统服务。
比如:1
2
3
4
5
6
7
8
9
10adb shell service list
Found 126 services:
0 sip: [android.net.sip.ISipService]
1 Genyd: [com.genymotion.genyd.IGenydService]
2 SystemPatcher: [com.genymotion.systempatcher.ISystemPatcher]
3 carrier_config: [com.android.internal.telephony.ICarrierConfigLoader]
4 phone: [com.android.internal.telephony.ITelephony]
...
43 notification: [android.app.INotificationManager]
...service check SERVICE用来检测指定的服务是否可用。
比如:1
2adb shell service check notification
Service notification: found表示
notification服务可用。service call命令用来调用服务中的方法,具体在下文单独说明。
service call 命令
service call SERVICE CODE [i32 N | i64 N | f N | d N | s16 STR ] ...
此命令用来调用服务中的方法,其中:
SERVICE代表服务名称;CODE代表所要调用的的服务中的方法在其aidl文件中对应的位置,第几个位置就调第几个方法;[i32 N | i64 N | f N | d N | s16 STR ] ...表示方法的参数,其中:i32表示int类型,i64表示long类型,f表示float类型,d表示double类型,s16表示String类型。
该命令语法如下:
1 | service call <your_service_name> <number at which the founction in your_service_name.aidl> <type of argument> <argument> |
那么 SERVICE 所代表的系统服务的 aidl 源文件到底在哪儿看呢?下文以 notification 系统服务为例。
首先调用
service list找到指定系统服务对应的aidl:1
2adb shell service list
43 notification: [android.app.INotificationManager]notification命令对应的系统服务的aidl是android.app.INotificationManager.aidl其次,在
Android的framework源码中找到对应的aidl文件,详细步骤为:android framework源码地址为:1
https://android.googlesource.com/platform/frameworks/base/
找到对应的系统版本,以
android-9.0.0_r18为例,该版本源码地址为:1
https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r18
android.app.INotificationManager.aidl对应的路径为/core/java/android/app/INotificationManager.aidl,找到地址:1
https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r18/core/java/android/app/INotificationManager.aidl
我们看一下 android-9.0.0_r18 版本的 INotificationManager.aidl 部分片段:
1 | interface INotificationManager |
cancelAllNotifications() 对应的 CODE 就是 1。
1
service call notification 1
表示清除当前所有的通知,其中
pkg,userId参数缺省。1
service call notification 1 s16 'com.example.package'
表示清除当前所有包名为
com.example.package的 App 发出的通知。1
service call notification 2 s16 'com.example.package' i32 108 i32 1
表示调用
clearData("com.example.package", 108, 1);其中布尔值true用132 1表示。
小结
service call CODE 中的 CODE 根据当前系统版本对应的 aidl 变化而变化,适用于调试,而应用于产品开发则需要充分考虑版本问题。
参考
Answer - Where to find description of all ‘/system/bin/service’ calls