当前位置: 移动技术网 > IT编程>开发语言>Java > 一键打开网络调试(无界面APP)源码

一键打开网络调试(无界面APP)源码

2020年07月08日  | 移动技术网IT编程  | 我要评论

一键打开网络调试(无界面APP)

开发环境AS4.0,kotlin1.72,java8

说明

为了开发调试方便
我们经常需要网络调试
蛋疼的是
每次关机又要重新打开网络调试
很麻烦,而且容易被累死
所以我开发了这
另外,此APP需要root权限
如果失败,那就老老实实插上USB连接电脑adb
运行:“adb tcpip 5555”

特点

1.把MainActivity做成了1像素,而且透明。打开网络调试后就关掉,这样用户就无感
2.桌面APP图标长按弹出对话框,进行端口设置

核心代码

  /**
     * 打开网络调试
     * @return 是否成功
     */
    fun openNetworkDebugging(): Boolean {
        var os: DataOutputStream? = null
        return try {
            val process = Runtime.getRuntime().exec("su")
            os = DataOutputStream(process.outputStream)
            os.writeBytes("setprop service.adb.tcp.port $PORT\n")
            os.writeBytes("start adbd\n")
            os.flush()
            true
        } catch (e: Exception) {
            false
        } finally {
            try {
                os?.close()
            } catch (e: IOException) {
            }
        }
    }

    /**
     * 关闭网络调试
     * @return 是否成功
     */
    fun closeNetworkDebugging(): Boolean {
        var os: DataOutputStream? = null
        return try {
            val process = Runtime.getRuntime().exec("su")
            os = DataOutputStream(process.outputStream)
            os.writeBytes("setprop service.adb.tcp.port 5555\n")
            os.writeBytes("stop adbd\n")
            os.flush()
            true
        } catch (e: Exception) {
            false
        } finally {
            try {
                os?.close()
            } catch (e: IOException) {
            }
        }
    }

效果图

在这里插入图片描述
在这里插入图片描述

我的github地址

我的csdn地址

源码下载

本文地址:https://blog.csdn.net/Yu1441/article/details/107182773

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网