Android 5.1 命令行更换以太网IP连接内网外 2020年版

公司的开发系统用的是安卓5.1,我在网上查大佬们的修改IP的教程;他们的支持库是V4,而我支持库用的是androidX,我下载了大佬们的源码,运行就报权限问题的错
在坑里爬了几天,最后换方式,使用命令行实现,最终成功了。
我不知道h废话少说,上菜

  1. 在系统的Ethernet里每次切换动态IP或静态IP的时候,以太网的网络图标都会消失,大概隔一秒才显示,这应该就是更换IP之后,需要重启以太网网络
    在这里插入图片描述
  2. 经过我调试当以太网关闭重新开启,网络图标都会消失,大概隔一秒才显示,就在开启还没显示网络图标的时候,离开修改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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值