android app代码中执行adb指令
短信预约 -IT技能 免费直播动态提醒
方案1:使用Runtime类
public static String execRootCmd(String cmd) { String content = ""; try { cmd = cmd.replace("adb shell",""); Process process = Runtime.getRuntime().exec(cmd); Log.d(TAG,"process " + process.toString()); content = process.toString(); } catch (IOException e) { Log.d(TAG,"exception " + e.toString()); e.printStackTrace(); } return content; }
方案二、
class Cmd { private val TAG = "Cmd" val result = StringBuilder() fun run(cmd: String): Boolean { var bufferedReader: BufferedReader? = null var dos: DataOutputStream? = null var receive = "" try { Runtime.getRuntime().exec("su")?.run { // 经过Root处理的android系统即有su命令 Logger.d("Cmd run: $cmd") bufferedReader = BufferedReader(InputStreamReader(inputStream)) dos = DataOutputStream(outputStream).apply { writeBytes(cmd + "\n") flush() writeBytes("exit\n") flush() } bufferedReader?.run { while (readLine().also { receive = it } != null) { result.append("\n").append(receive) } } waitFor() } } catch (e: Exception) { return false } try { dos?.close() bufferedReader?.close() } catch (e: Exception) { return false } return true }}
Cmd().run("pm install -r $basePath/APK/$apkName.apk")//静默安装
来源地址:https://blog.csdn.net/liuqinhou/article/details/129168130
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341