公司的开发系统用的是安卓5.1,我在网上查大佬们的修改IP的教程;他们的支持库是V4,而我支持库用的是androidX,我下载了大佬们的源码,运行就报权限问题的错
在坑里爬了几天,最后换方式,使用命令行实现,最终成功了。
我不知道h废话少说,上菜
- 在系统的Ethernet里每次切换动态IP或静态IP的时候,以太网的网络图标都会消失,大概隔一秒才显示,这应该就是更换IP之后,需要重启以太网网络
- 经过我调试当以太网关闭重新开启,网络图标都会消失,大概隔一秒才显示,就在开启还没显示网络图标的时候,离开修改IP,它才可以连接内网网(慢了那就连不了外网了)。命令行,关闭以太网,开启以太网,修改IP。(注:需要ROOT)
(1)主要命令行
注:需要先把设备的Ethernet调为自动IP
//关闭以太网
ifconfig eth0 down
//开启以太网
ifconfig eth0 up
//设置IP
busybox ifconfig eth0 192.168.0.133 netmask 255.255.255.0 up
(2)代码ShellUtils.java
public class ShellUtils {
public static final String COMMAND_SU = "su";
public static final String COMMAND_SH = "sh";
public static final String COMMAND_EXIT = "exit\n";
public static final String COMMAND_LINE_END = "\n";
//这一段看个人情况,我不需要
/*public ShellUtils() {
throw new AssertionError();
}*/
/**
* 查看是否有了root权限
*
* @return
*/
public static boolean checkRootPermission() {
return execCommand("echo root", true, false).result == 0;
}
/**
* 执行shell命令,默认返回结果
*
* @param command
* command
* @param
* //运行是否需要root权限
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot) {
return execCommand(new String[] {
command }, isRoot, true);
}
/**
* 执行shell命令,默认返回结果
*
* @param commands
* command list
* @param
* //运行是否需要root权限
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List<String> commands,
boolean isRoot) {
return execCommand(
commands == null ? null : commands.toArray(new String[] {
}),
isRoot, true