adb shell svc 详解
这个命令可以用来控制电源管理,wifi开关,数据开关(就是上网流量),注意需要root权限。
有兴趣的可以一起学习一下。
一、svc 常用命令:
1、屏幕相关
adb shell svc power stayon [true|false|usb|ac]
设置屏幕的常亮,true保持常亮,false不保持,usb当插入usb时常亮,ac当插入电源时常亮
2、移动数据(sim卡网络)
adb shell svc data enable/打开移动数据
adb shell svc data disable/关闭移动数据
adb shell svc data prefer/设置移动数据优先
3、wifi相关
adb shell svc wifi enable/打开wifi
adb shell svc wifi disable/关闭wifi
adb shell svc wifi prefer/设置wifi优先
4、蓝牙相关
adb shell svc bluetooth enable/打开wifi
adb shell svc bluetooth disable/关闭wifi
此命令可以用作4G/wifi稳定性测试
其实还有其他svc 命令,如果想深入了解可以往后看看。
二、svc 命令和使用示例:
查看系统是否安装了svc
路径 /system/bin/svc
1|console:/ # ls -l /system/bin/svc
-rwxr-xr-x 1 root shell 1697 2023-09-08 09:06 /system/bin/svc
console:/ #
如果存在这个 svc 文件,那就就可以执行 svc 命令了。
其实可以cat看到 svc 文件的代码,具体是哪里生成和怎么编辑修改,这里不做具体分析。
1、svc
在串口或者adb shell 命令行,输入svc 就可以看到相关功能,
也可能看不全,可能跟系统或者svc文件相关。
console:/ # svc
Available commands:
help Show information about the subcommands
power Control the power manager
usb Control Usb state
nfc Control NFC functions
system-server System server process related command
console:/ #
console:/ #
在串口输入 svc 就能看到相关指令功能。但是不一定显示全。
2、svc help
console:/ # svc help
Available commands:
help Show information about the subcommands
power Control the power manager
usb Control Usb state
nfc Control NFC functions
system-server System server process related command
console:/ #
输入 svc help ,查看其实没啥用,和 svc 一样的显示功能列表。
3、svc power
console:/ # svc power
Control the power manager
usage: svc power stayon [true|false|usb|ac|wireless]
Set the 'keep awake while plugged in' setting.
svc power reboot [reason]
Perform a runtime shutdown and reboot device with specified reason.
svc power shutdown
Perform a runtime shutdown and power off the device.
svc power forcesuspend [t]
Force the system into suspend, ignoring all wakelocks.
t - Number of milliseconds to wait before issuing force-suspend.
Helps with devices that can't suspend while plugged in.
Defaults to 0.
When using a delay, you must use the nohup shell modifier:
'adb shell nohup svc power forcesuspend [time]'
Use caution; this is dangerous. It puts the device to sleep
immediately without giving apps or the system an opportunity to
save their state.
console:/ #
这里能看到 avc power 包含下面几个功能:
svc power stayon [true|false|usb|ac|wireless] //对应的意义:[长亮|长暗|usb接入长亮|充电长亮|连接wifi长亮]
svc power reboot [reason] //[reason] 可以随便写或者不写,底层估计是会记录
svc power shutdown //关机
svc power forcesuspend [time]// 定时关机,其实也是关机,time 是多久后关机,单位是毫秒。未设置time,就是马上关机。
3、svc usb
console:/ #
console:/ # svc usb
Control Usb state
usage: svc usb setFunctions [function]
Set the current usb function. If function is blank, sets to charging.
svc usb setScreenUnlockedFunctions [function]
Sets the functions which, if the device was charging,
become current on screen unlock.
If function is blank, turn off this feature.
svc usb getFunctions
Gets the list of currently enabled functions
possible values of [function] are any of 'mtp', 'ptp', 'rndis',
'midi', 'ncm (if supporting gadget hal v1.2)'
svc usb resetUsbGadget
Reset usb gadget
svc usb getUsbSpeed
Gets current USB speed
possible values of USB speed are any of 'low speed', 'full speed',
'high speed', 'super speed', 'super speed (10G)',
'super speed (20G)', or higher (future extension)
svc usb getGadgetHalVersion
Gets current Gadget Hal Version
possible values of Hal version are any of 'unknown', 'V1_0', 'V1_1',
'V1_2'
svc usb getUsbHalVersion
Gets current USB Hal Version
possible values of Hal version are any of 'unknown', 'V1_0', 'V1_1',
'V1_2', 'V1_3'
svc usb resetUsbPort [port number]
Reset the specified connected usb port
default: the first connected usb port
console:/ #
console:/ #
这上面看 svc usb 看起来太复杂了吧!
梳理下,主要包含下面几个功能:
svc usb setFunctions [function] //设置充电,还是文件等模式
svc usb setScreenUnlockedFunctions [function]
svc usb resetUsbGadget
svc usb getUsbSpeed
svc usb getGadgetHalVersion
svc usb getUsbHalVersion
svc usb resetUsbPort [port number]
其实上面svc usb执行后,都在UsbCommand 里面被接收,部分方法调用是 UsbDeviceManager 里面执行。
framework\base\cmds\svc\src\com\android\commands\svc\UsbCommand.java
framework\base\services\usb\java\com\android\server\usb\UsbDeviceManager.java
想要仔细研究的可以自己看看。
4、svc nfc
console:/ # svc nfc
Got a null NfcAdapter, is the system running?
console:/ #
这里看到,设备没有安装ngc,所以未查询到任何信息。
5、svc wifi
127|console:/ # svc wifi
Control the Wi-Fi manager
usage: svc wifi [enable|disable]
Turn Wi-Fi on or off.
1|console:/ #
这里看到, avc wifi 可以通过 enable/disable 控制wifi开关。
这里没看到 svc wifi prefer/设置wifi优先!
我这是tv平台,不会插sim卡,可能去除了相关设置,入需要具体研究可以看svc文件和 wifi 命令相关文件。
packages\modules\Wifi\service\java\com\android\server\wifi\WifiShellCommand.java
6、svc bluetooth
1|console:/ # svc bluetooth
Control the Bluetooth manager
usage: svc bluetooth [enable|disable]
Turn Bluetooth on or off.
1|console:/ #
这里看到, avc bluetooth 可以通过 enable/disable 控制 蓝牙 开关。
7、svc system-server
console:/ # svc system-server
System server process related command
usage: system-server wait-for-crash
Wait until the system server process crashes.
console:/ #
这里显示等下系统崩溃,估计系统崩溃过情况才有日志打印。
8、svc data
1|console:/ # svc data
Enable/Disable Mobile Data Connectivity
usage: svc data [enable|disable]
1|console:/ #
可以通过svc data enable或svc data disable来打开或关闭移动数据连接。这个命令可以用来控制手机的数据连接状态。
这里没有查询命令,只能通过实际测试看看,是否有没有生效。
9、svc volume
网上查看svc 还可以控制变量:
adb shell svc volume music +10 // 将音乐音量增加10个单位
但是实际测试,并不能识别 volume 命令,估计和Android 系统代码相关
console:/ # svc volume music +10
Available commands:
help Show information about the subcommands
power Control the power manager
usb Control Usb state
nfc Control NFC functions
system-server System server process related command
console:/ # svc volume music
Available commands:
help Show information about the subcommands
power Control the power manager
usb Control Usb state
nfc Control NFC functions
system-server System server process related command
console:/ # svc volume
Available commands:
help Show information about the subcommands
power Control the power manager
usb Control Usb state
nfc Control NFC functions
system-server System server process related command
console:/ #
console:/ #
这里看到是识别不到 svc volume命令的,其实就是系统代码未配置,想要了解可以继续往下看。
二、svc 命令相关源码分析
1、cat svc文件
console:/ #
console:/ # cat /system/bin/svc
#!/system/bin/sh
# `svc wifi` has been migrated to WifiShellCommand,
# simply perform translation to `cmd wifi set-wifi-enabled` here.
if [ "x$1" == "xwifi" ]; then
# `cmd wifi` by convention uses enabled/disabled
# instead of enable/disable
if [ "x$2" == "xenable" ]; then
exec cmd wifi set-wifi-enabled enabled
elif [ "x$2" == "xdisable" ]; then
exec cmd wifi set-wifi-enabled disabled
else
echo "Control the Wi-Fi manager"
echo ""
echo "usage: svc wifi [enable|disable]"
echo " Turn Wi-Fi on or off."
echo ""
fi
exit 1
fi
if [ "x$1" == "xdata" ]; then
if [ "x$2" == "xenable" ]; then
exec cmd phone data enable
elif [ "x$2" == "xdisable" ]; then
exec cmd phone data disable
else
echo "Enable/Disable Mobile Data Connectivity"
echo ""
echo "usage: svc data [enable|disable]"
echo ""
fi
exit 1
fi
# `svc bluetooth` has been migrated to BluetoothShellCommand,
# simply perform translation to `cmd bluetooth set-bluetooth-enabled` here.
if [ "x$1" == "xbluetooth" ]; then
# `cmd wifi` by convention uses enabled/disabled
# instead of enable/disable
if [ "x$2" == "xenable" ]; then
exec cmd bluetooth_manager enable
elif [ "x$2" == "xdisable" ]; then
exec cmd bluetooth_manager disable
else
echo "Control the Bluetooth manager"
echo ""
echo "usage: svc bluetooth [enable|disable]"
echo " Turn Bluetooth on or off."
echo ""
fi
exit 1
fi
export CLASSPATH=/system/framework/svc.jar
exec app_process /system/bin com.android.commands.svc.Svc "$@"
console:/ #
这里看到了有wifi和bluetooth的简单判断,以及关联了svc.jar
Android 系统源码里面也是有这个svc文件存在的,文件目录:
framework\base\cmds\svc\svc
svc相关功能的接收处理都在 framework\base\cmds\svc 文件夹下
2、svc 相关源码文件
主要相关文件:
framework\base\cmds\svc\src\com\android\commands\svc\Svc.java
framework\base\cmds\svc\src\com\android\commands\svc\NfcCommand.java
framework\base\cmds\svc\src\com\android\commands\svc\PowerCommand.java
framework\base\cmds\svc\src\com\android\commands\svc\SystemServerCommand.java
framework\base\cmds\svc\src\com\android\commands\svc\UsbCommand.java
packages\modules\Wifi\service\java\com\android\server\wifi\WifiShellCommand.java // Android13
packages\modules\Bluetooth\service\java\com\android\server\bluetooth\BluetoothShellCommand.java //Android13
别人分析的svc相关文件 过程:
https://blog.csdn.net/zhangqi6627/article/details/121496414
(1)接收所有svc命令的文件 Svc.java
public class Svc {
public static abstract class Command { // 所有command 子类都是会继承这个抽象接口,后面会看到
private String mName;
public Command(String name) { //设置Command 名称
mName = name;
}
public String name() { //查询Command名称
return mName;
}
public abstract String shortHelp(); // should fit on one short line, //svc help看到的各个Command 的信息
public abstract String longHelp(); // take as much space as you need, 75 col max // svc 具体命令后,看到的具体提示信息
public abstract void run(String[] args); // run the command //执行接收输入的命令
}
// (1)执行入口
public static void main(String[] args) {
if (args.length >= 1) {
Command c = lookupCommand(args[0]); //(2)根据输入的字符串,返回Command 对象
if (c != null) {
c.run(args);
return;
}
}
COMMAND_HELP.run(args); // (3)如果只输入 avc,显示help 提示的字符串
}
// (4)根据字符串查找 Command 对象,其实就是USB、Wifi那些控制对象
private static Command lookupCommand(String name) {
final int N = COMMANDS.length;
for (int i=0; i<N; i++) { // (5)遍历所有Command对象进行匹配
Command c = COMMANDS[i];
if (c.name().equals(name)) {
return c;
}
}
return null;
}
// (5)定义所有的 Command 对象
public static final Command[] COMMANDS = new Command[] {
COMMAND_HELP, // (6)help 是自身
new PowerCommand(), // (7) power相关命令处理
// `svc wifi` has been migrated to WifiShellCommand // (8)wifi 相关命令处理,svc 文件已经说明了,会在 WifiShellCommand 进行处理。
new UsbCommand(), // (9) usb相关命令处理
new NfcCommand(), // (10)nfc相关命令处理
// `svc bluetooth` has been migrated to BluetoothShellCommand // (11)bluetooth相关命令处理
new SystemServerCommand(), // (11)system-server 命令处理
};
}
(2)接收所有svc power 命令的文件 PowerCommand.java
这里只说明一下 avc power 命令的接收和处理,其他命令的 Command 命令类的分析也是类似的,可以参考。
public class PowerCommand extends Svc.Command { //(1) svc 命令都是要继承 Command抽象类
private static final int FORCE_SUSPEND_DELAY_DEFAULT_MILLIS = 0;
public PowerCommand() {
super("power"); // (2)父类的构成方法,设置Command 对象名称,也是svc 后面对应的功能名称
}
public String shortHelp() { // (3)svc 显示的 power 功能简单提示
return "Control the power manager";
}
public String longHelp() { // (4)svc power 显示的 power 功能具体提示,这个也说明文本提示因供参考,真正的实现还是要看代码逻辑
return shortHelp() + "\n"
+ "\n"
+ "usage: svc power stayon [true|false|usb|ac|wireless]\n"
+ " Set the 'keep awake while plugged in' setting.\n"
+ " svc power reboot [reason]\n"
+ " Perform a runtime shutdown and reboot device with specified reason.\n"
+ " svc power shutdown\n"
+ " Perform a runtime shutdown and power off the device.\n"
+ " svc power forcesuspend [t]\n"
+ " Force the system into suspend, ignoring all wakelocks.\n"
+ " t - Number of milliseconds to wait before issuing force-suspend.\n"
+ " Helps with devices that can't suspend while plugged in.\n"
+ " Defaults to " + FORCE_SUSPEND_DELAY_DEFAULT_MILLIS + ".\n"
+ " When using a delay, you must use the nohup shell modifier:\n"
+ " 'adb shell nohup svc power forcesuspend [time]'\n"
+ " Use caution; this is dangerous. It puts the device to sleep\n"
+ " immediately without giving apps or the system an opportunity to\n"
+ " save their state.\n";
}
//(5)这个就是 svc power 功能的具体处理代码了,也是最重要的部分
public void run(String[] args) {
fail: {
if (args.length >= 2) { //(6)命令字符串大于等于2 才有意义, args[0] = power, args[1] = 某个功能,比如 stayon ,args[2] = 某个功能的参数
IPowerManager pm = IPowerManager.Stub.asInterface(
ServiceManager.getService(Context.POWER_SERVICE)); //(7)具体实现其实也是调用到了PowerManager对于的Service 服务里面,也就是说avc 里面的实现基本都是调用系统api的操作
if ("stayon".equals(args[1]) && args.length == 3) { // (8)avc power stayon 功能的判断
int val;
if ("true".equals(args[2])) { //(9)avc power stayon true 的判断,长亮
val = BatteryManager.BATTERY_PLUGGED_AC |
BatteryManager.BATTERY_PLUGGED_USB |
BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
else if ("false".equals(args[2])) { //(10)avc power stayon false 的判断,长暗
val = 0;
} else if ("usb".equals(args[2])) { //(11)avc power stayon usb 的判断,插入usb长亮
val = BatteryManager.BATTERY_PLUGGED_USB;
} else if ("ac".equals(args[2])) { //(12)avc power stayon ac 的判断,插入ac电源长亮
val = BatteryManager.BATTERY_PLUGGED_AC;
} else if ("wireless".equals(args[2])) { //(13)avc power stayon wireless 的判断,连接wifi 长亮
val = BatteryManager.BATTERY_PLUGGED_WIRELESS;
} else { //(14)avc power 其他参数都是,错误情况,进行提示
break fail;
}
try {
if (val != 0) {
// if the request is not to set it to false, wake up the screen so that
// it can stay on as requested
pm.wakeUp(SystemClock.uptimeMillis(),
PowerManager.WAKE_REASON_UNKNOWN, "PowerCommand", null);
}
pm.setStayOnSetting(val); //(15)根据 avc power 设置的功能,调用相关api,设置相关参数,
}
catch (RemoteException e) {
System.err.println("Faild to set setting: " + e);
}
return;
} else if ("reboot".equals(args[1])) { //(16)后面的流程就不一一分析了,参数长度和具体调用的api不同而已。
String mode = null;
if (args.length == 3) {
mode = args[2];
}
try {
// no confirm, wait till device is rebooted
pm.reboot(false, mode, true);
} catch (RemoteException e) {
maybeLogRemoteException("Failed to reboot.");
} catch (Exception e) {
System.err.println("Failed to reboot: " + e.getMessage());
}
return;
} else if ("shutdown".equals(args[1])) {
try {
// no confirm, wait till device is off
pm.shutdown(false, null, true);
} catch (RemoteException e) {
maybeLogRemoteException("Failed to shutdown.");
}
return;
} else if ("forcesuspend".equals(args[1])) {
int delayMillis = args.length > 2
? Integer.parseInt(args[2]) : FORCE_SUSPEND_DELAY_DEFAULT_MILLIS;
try {
Thread.sleep(delayMillis);
if (!pm.forceSuspend()) {
System.err.println("Failed to force suspend.");
}
} catch (InterruptedException e) {
System.err.println("Failed to force suspend: " + e);
} catch (RemoteException e) {
maybeLogRemoteException("Failed to force-suspend with exception: " + e);
}
return;
}
}
}
System.err.println(longHelp()); // (17)如果参数错误的情况,就显示avc power 的具体命令提示。
}
三、系统源码中添加自定义的svc 命令实现
1、系统源码中加入新的svc 只需要做下面两步即可:
(1)创建一个自己 Command 对象,可以参考同目录下的其他Command对象,比如 PowerCommand 的代码;
(2)在 Svc.java 文件中 COMMANDS列表对象中添加自定义的 Command 对象,svc 遍历的时候就会判断是否包含你的命令
2、在 Svc.java 中的修改
public static final Command[] COMMANDS = new Command[] {
COMMAND_HELP,
new LwzCommand(), //这里添加自定定义的 Command 对象
new PowerCommand(),
// `svc wifi` has been migrated to WifiShellCommand
new UsbCommand(),
new NfcCommand(),
// `svc bluetooth` has been migrated to BluetoothShellCommand
new SystemServerCommand(),
};
输入svc 命令的时候,就会去遍历所有的 Command 对象的名称,
哪个符合就返回那个具体的Command对象,具体的处理都在 某个Command对象里面。
3、创建自定义的 Command 对象
package com.android.commands.svc;
import android.content.Context;
public class LwzCommand extends Svc.Command { //(1)自定义Command对象,需要继承 Svc.Command
private static final int FORCE_SUSPEND_DELAY_DEFAULT_MILLIS = 0;
public LwzCommand() { //(2)自定义对象的命令名称,这个也是cmd 窗口中的功能命令,Java那个对象命名是无所谓的
super("lwz");
}
public String shortHelp() { //(3)简短的介绍,avc 、avc help 会出现该提示
return "lwz Control the test command";
}
public String longHelp() { //(4)详细的介绍,avc lwz 或者 avc lwz help ,会打印该提示
return shortHelp() + "\n"
+ "\n"
+ "usage: svc lwz stayon [true|false|usb|ac|wireless]\n"
+ " Set the 'keep awake while plugged in' setting.\n"
+ " svc lwz reboot [reason]\n"
+ " Perform a runtime shutdown and reboot device with specified reason.\n"
+ " svc lwz shutdown\n";
}
public void run(String[] args) { //(4)avc lwz XXX 的具体处理
fail: {
if (args.length >= 2) { //(5)avc lwz stayon 的具体处理,这里只是做了一些打印,未调用具体的api实现
if ("stayon".equals(args[1]) && args.length == 3) {
int val;
if ("true".equals(args[2])) {
val = 100;
}
else if ("false".equals(args[2])) {
val = 0;
} else if ("usb".equals(args[2])) {
val = 1;
} else if ("ac".equals(args[2])) {
val = 2;
} else if ("wireless".equals(args[2])) {
val = 3;
} else {
break fail;
}
System.err.println("test avc lwz command :" + args[2] + ", val = " + val); //添加了自定义打印
return;
} else if ("reboot".equals(args[1])) { //(6)avc lwz reboot 的具体处理,这里只是做了一些打印,未调用具体的api实现
String mode = null;
if (args.length == 3) {
mode = args[2];
}
System.err.println("test avc lwz command :" + args[1] + ", mode = " + mode);
return;
}
//(6)avc lwz XXX 其他输入的处理,打印具体介绍字符串
System.err.println(longHelp());
}
}
System.err.println(longHelp());
}
}
上面自定义的Command对象,未调用具体的api。
实际应用中是可以调用系统api的,可以打印返回的数据,写线程访问网络都是没有问题的。
4、添加自定义Command命令后,串口操作的显示
下面是添加了自定义LwzCommand.java 文件后,串口的打印日志:
console:/ #
console:/ # svc //(1)输入svc 可以看到添加了 自定义的 lwz 功能命令
Available commands:
help Show information about the subcommands
lwz lwz Control the test command
power Control the power manager
usb Control Usb state
nfc Control NFC functions
system-server System server process related command
console:/ #
console:/ # svc lwz //(2)自定义的功能简介,参考 svc power 的功能编写的
lwz Control the test command //(3)添加的打印
usage: svc lwz stayon [true|false|usb|ac|wireless]
Set the 'keep awake while plugged in' setting.
svc lwz reboot [reason]
Perform a runtime shutdown and reboot device with specified reason.
svc lwz shutdown
console:/ #
console:/ #
console:/ # svc lwz shutdown // (4)avc lwz shutdown 实际代码未实现,代码中打印了Help提示
lwz Control the test command
usage: svc lwz stayon [true|false|usb|ac|wireless]
Set the 'keep awake while plugged in' setting.
svc lwz reboot [reason]
Perform a runtime shutdown and reboot device with specified reason.
svc lwz shutdown
console:/ #
console:/ #
console:/ # svc lwz reboot // (5)avc lwz reboot 代码中只是添加了打印,未做处理
test avc lwz command :reboot, mode = null
console:/ #
console:/ #
console:/ # svc lwz stayon true // (6)avc lwz stayon true 代码中只是添加了打印,未做其他处理
test avc lwz command :true, val = 100
console:/ #
console:/ #
console:/ # svc lwz stayon false // (7)avc lwz stayon false 代码中只是添加了打印,未做其他处理
test avc lwz command :false, val = 0
console:/ #
这里看效果还是比较ok 的。
即学到了 svc 的相关内容,还学会了自定义扩展知识。
四、总结
1、svc 的使用
svc 主要命令:
adb shell svc power/wifi/bluetooth/usb/nfc
从上面介绍可以知道,其实只要记住 svc,那么就是提示其他相关命令,然后再输入功能命令,就会显示具体参数介绍。
2、svc 设计自定义命令
其实不难,一个是编写自定义的Command类,还有就是把这个类对象加入到Svc.java中。
涉及到的修改:
framework\base\cmds\svc\src\com\android\commands\svc\Svc.java
framework\base\cmds\svc\src\com\android\commands\svc\XXXCommand.java
3、其他相关知识
(1)有些串口上显示,svc 是无效的
在串口中的打印如下:
1|console:/ # svc
CANNOT LINK EXECUTABLE "app_process": library "libnativeloader.so" not found: needed by main executable
1|console:/ #
1|console:/ #
我这里的311D2 13系统是这样提示的,其他的系统不清楚是怎么的提示。
但是svc文件是存在的,adb shell 的cmd 窗口是ok 的;这个不清楚和哪里相关。
(2)cmd 其他的命令延伸说明
其实系统源码里面除了 svc 还有非常多的串口调试命令。
在系统源码目录 frameworks\base\cmds 可以看到:
Android13-IFP/release$
Android13-IFP/release$ cd frameworks/base/cmds
Android13-IFP/release/frameworks/base/cmds$ ls
abx appops appwidget bmgr bu device_config hid ime incidentd input locksettings requestsync settings svc uiautomator vr
am app_process backup bootanimation content dpm idmap2 incident incident_helper interrupter pm screencap sm telecom uinput wm
Android13-IFP/release/frameworks/base/cmds$
这里看到里面还有30多个 cmd 命令。比如控制应用的 am/pm 命令,控制显示的 wm 命令等等。
这些命令有些是需要 root 权限,有些是不需要的.
但是也不是所有的 cmd 命名都定义在这里,比如 ifconfig 这个命令就不知道是在哪里实现的。文章来源:https://www.toymoban.com/news/detail-703554.html
到这里,svc 和相关知识已经是了解比如深入了的。文章来源地址https://www.toymoban.com/news/detail-703554.html
到了这里,关于Android adb shell svc 知识详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!