介绍
service命令用来查询某一个服务是否启动,以及调用某一个服务中的一个方法。
使用说明
#service
Usage: service [-h|-?]
service list
service check SERVICE
service call SERVICE CODE [i32 N | i64 N | f N | d N | s16 STR ] ...
Options:
i32: Write the 32-bit integer N into the send parcel.
i64: Write the 64-bit integer N into the send parcel.
f: Write the 32-bit single-precision number N into the send parcel.
d: Write the 64-bit double-precision number N into the send parcel.
s16: Write the UTF-16 string STR into the send parcel.
#service list
Found 104 services:
0 sip: [android.net.sip.ISipService]
1 carrier_config: [com.android.internal.telephony.ICarrierConfigLoader]
2 phone: [com.android.internal.telephony.ITelephony]
3 isms: [com.android.internal.telephony.ISms]
4 iphonesubinfo: [com.android.internal.telephony.IPhoneSubInfo]
5 simphonebook: [com.android.internal.telephony.IIccPhoneBook]
6 isub: [com.android.internal.telephony.ISub]
7 telecom: [com.android.internal.telecom.ITelecomService]
8 imms: [com.android.internal.telephony.IMms]
9 media_projection: [android.media.projection.IMediaProjectionManager]
10 launcherapps: [android.content.pm.ILauncherApps]
11 fingerprint: [android.hardware.fingerprint.IFingerprintService]
12 trust: [android.app.trust.ITrustManager]
13 media_router: [android.media.IMediaRouterService]
14 media_session: [android.media.session.ISessionManager]
15 restrictions: [android.content.IRestrictionsManager]
16 print: [android.print.IPrintManager]
17 graphicsstats: [android.view.IGraphicsStats]
18 assetatlas: [android.view.IAssetAtlas]
19 dreams: [android.service.dreams.IDreamManager]
20 commontime_management: []
21 samplingprofiler: []
22 diskstats: []
23 voiceinteraction: [com.android.internal.app.IVoiceInteractionManagerService]
24 appwidget: [com.android.internal.appwidget.IAppWidgetService]
25 backup: [android.app.backup.IBackupManager]
26 jobscheduler: [android.app.job.IJobScheduler]
27 serial: [android.hardware.ISerialManager]
28 usb: [android.hardware.usb.IUsbManager]
29 midi: [android.media.midi.IMidiManager]
30 DockObserver: []
31 audio: [android.media.IAudioService]
32 wallpaper: [android.app.IWallpaperManager]
33 dropbox: [com.android.internal.os.IDropBoxManagerService]
34 search: [android.app.ISearchManager]
35 country_detector: [android.location.ICountryDetector]
36 location: [android.location.ILocationManager]
37 devicestoragemonitor: []
38 notification: [android.app.INotificationManager]
39 updatelock: [android.os.IUpdateLock]
40 servicediscovery: [android.net.nsd.INsdManager]
41 connectivity: [android.net.IConnectivityManager]
42 ethernet: [android.net.IEthernetManager]
43 rttmanager: [android.net.wifi.IRttManager]
44 wifiscanner: [android.net.wifi.IWifiScanner]
45 wifi: [android.net.wifi.IWifiManager]
46 wifip2p: [android.net.wifi.p2p.IWifiP2pManager]
47 netpolicy: [android.net.INetworkPolicyManager]
48 netstats: [android.net.INetworkStatsService]
49 network_score: [android.net.INetworkScoreService]
50 textservices: [com.android.internal.textservice.ITextServicesManager]
51 network_management: [android.os.INetworkManagementService]
52 clipboard: [android.content.IClipboard]
53 statusbar: [com.android.internal.statusbar.IStatusBarService]
54 device_policy: [android.app.admin.IDevicePolicyManager]
55 deviceidle: [android.os.IDeviceIdleController]
56 persistent_data_block: [android.service.persistentdata.IPersistentDataBlockService]
57 lock_settings: [com.android.internal.widget.ILockSettings]
58 uimode: [android.app.IUiModeManager]
59 mount: [IMountService]
60 accessibility: [android.view.accessibility.IAccessibilityManager]
61 input_method: [com.android.internal.view.IInputMethodManager]
62 bluetooth_manager: [android.bluetooth.IBluetoothManager]
63 input: [android.hardware.input.IInputManager]
64 window: [android.view.IWindowManager]
65 alarm: [android.app.IAlarmManager]
66 consumer_ir: [android.hardware.IConsumerIrService]
67 vibrator: [android.os.IVibratorService]
68 DynamicPManager: [android.os.IDynamicPManager]
69 content: [android.content.IContentService]
70 account: [android.accounts.IAccountManager]
71 media.camera.proxy: [android.hardware.ICameraServiceProxy]
72 telephony.registry: [com.android.internal.telephony.ITelephonyRegistry]
73 scheduling_policy: [android.os.ISchedulingPolicyService]
74 webviewupdate: [android.webkit.IWebViewUpdateService]
75 usagestats: [android.app.usage.IUsageStatsManager]
76 battery: []
77 sensorservice: [android.gui.SensorServer]
78 processinfo: [android.os.IProcessInfoService]
79 permission: [android.os.IPermissionController]
80 cpuinfo: []
81 dbinfo: []
82 gfxinfo: []
83 meminfo: []
84 procstats: [com.android.internal.app.IProcessStats]
85 activity: [android.app.IActivityManager]
86 user: [android.os.IUserManager]
87 package: [android.content.pm.IPackageManager]
88 media.radio: [android.hardware.IRadioService]
89 media.sound_trigger_hw: [android.hardware.ISoundTriggerHwService]
90 media.audio_policy: [android.media.IAudioPolicyService]
91 media.camera: [android.hardware.ICameraService]
92 display: [android.hardware.display.IDisplayManager]
93 power: [android.os.IPowerManager]
94 appops: [com.android.internal.app.IAppOpsService]
95 batterystats: [com.android.internal.app.IBatteryStats]
96 batteryproperties: [android.os.IBatteryPropertiesRegistrar]
97 media.resource_manager: [android.media.IResourceManagerService]
98 media.player: [android.media.IMediaPlayerService]
99 media.audio_flinger: [android.media.IAudioFlinger]
100 drm.drmManager: [drm.IDrmManagerService]
101 SurfaceFlinger: [android.ui.ISurfaceComposer]
102 android.security.keystore: [android.security.IKeystoreService]
103 android.service.gatekeeper.IGateKeeperService: [android.service.gatekeeper.IGateKeeperService]
将系统服务列出来,共找到104个服务
格式(以wifi为例): wifi: [android.net.wifi.IWifiManager]
服务名 : 服务实现的aidl接口
服务名在Context.java中有定义,Context.java源码
服务接口aidl在IWifiManager.aidl中有定义,IWifiManager.aidl源码
#service check wifi
Service wifi: found
查看wifi服务
#service call SERVICE CODE 解释如下
#service call 服务名 服务函数序号 参数(i32 32位整型,i64整型,s16 字符串, f N , d N 猜测应该是float和 double类型)
服务函数序号(注意和Android版本有关,不同版本函数序号可能不一样):
关于函数序号,java类在编译成字节码后,所有函数名都会去掉,它们会按顺序放在一个函数数组中,方法变成了method1,method2....
这里调用方法以打开和关闭wifi为例(Android6.0),调用 IWifiManager.aidl中的
boolean setWifiEnabled(boolean enable) ,在IWifiManager.aidl中,setWifiEnabled是第19号函数,那么我们通过service打开和关闭wifi,操作如下:
1. 查看wifi
#ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope: Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 TX bytes:0
wlan0 Link encap:Ethernet HWaddr 6C:21:A2:DF:73:D6
inet addr:192.168.1.4 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: 2409:8a34:11e:7520:c534:d888:fb74:ecb0/64 Scope: Global
inet6 addr: 2409:8a34:11e:7520:6e21:a2ff:fedf:73d6/64 Scope: Global
inet6 addr: fe80::6e21:a2ff:fedf:73d6/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:495 errors:0 dropped:7 overruns:0 frame:0
TX packets:122 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:159960 TX bytes:12916
p2p0 Link encap:Ethernet HWaddr 6E:21:A2:DF:73:D6
inet6 addr: fe80::6c21:a2ff:fedf:73d6/64 Scope: Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 TX bytes:0
2. 关闭wifi
#service call wifi 19 i32 0
Result: Parcel(00000000 00000001 '........')
#ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope: Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 TX bytes:0
关闭wifi成功
3. 打开wifi
#service call wifi 19 i32 1
Result: Parcel(00000000 00000001 '........')
#ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope: Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 TX bytes:0
wlan0 Link encap:Ethernet HWaddr 6C:21:A2:DF:73:D6
inet addr:192.168.1.4 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: 2409:8a34:11e:7520:c534:d888:fb74:ecb0/64 Scope: Global
inet6 addr: 2409:8a34:11e:7520:6e21:a2ff:fedf:73d6/64 Scope: Global
inet6 addr: fe80::6e21:a2ff:fedf:73d6/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:512 errors:0 dropped:9 overruns:0 frame:0
TX packets:135 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:162882 TX bytes:14598
p2p0 Link encap:Ethernet HWaddr 6E:21:A2:DF:73:D6
inet6 addr: fe80::6c21:a2ff:fedf:73d6/64 Scope: Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 TX bytes:0
打开wifi成功
4. 获取连接信息
WifiInfo getConnectionInfo(); 在IWifiManager.aidl中18号函数,调用如下:
#service call wifi 18
Result: Parcel(
0x00000000: 00000000 00000001 00000002 ffffffbd '................'
0x00000010: 00000048 000009a8 00000001 00000004 'H...............'
0x00000020: 0401a8c0 00000001 00000009 00000009 '................'
0x00000030: 43434d43 51326d2d 00000032 00000011 'CMCC-m2Q2.......'
0x00000040: 00300031 0064003a 003a0064 00610034 '1.0.:.d.d.:.4.a.'
0x00000050: 0034003a 003a0063 00610038 0035003a ':.4.c.:.8.a.:.5.'
0x00000060: 00000031 00000011 00320030 0030003a '1.......0.2.:.0.'
0x00000070: 003a0030 00300030 0030003a 003a0030 '0.:.0.0.:.0.0.:.'
0x00000080: 00300030 0030003a 00000030 00000000 '0.0.:.0.0.......'
0x00000090: 00000000 0000003c 00000000 3cce1800 '....<..........<'
0x000000a0: 00000000 00000000 00000000 00000000 '................'
0x000000b0: dc000f88 3f4801b5 00000000 00000000 '......H?........'
0x000000c0: 00000009 004f0043 0050004d 0045004c '....C.O.M.P.L.E.'
0x000000d0: 00450054 00000044 'T.E.D... ')