我可以通过程序(根)从系统对话框中选择项目吗?

时间:2019-05-07 07:08:43

标签: android

我可以通过以下方式打开“使用USB连接至”系统对话框:

val p = Runtime.getRuntime().exec(arrayOf("su", "-c", "system/bin/sh"))
val stdout = DataOutputStream(p.outputStream)
stdout.writeBytes("am start com.android.settings/.UsbSettings")
stdout.flush()
stdout.close()

我还想通过编程方式选择任何选项(例如“传输文件”)?

扎根的Android是否有可能?

enter image description here

更新

似乎我可以玩input keyevent https://stackoverflow.com/a/28969112/9766649(例如61 --> "KEYCODE_TAB"62 --> "KEYCODE_SPACE"很有用)

1 个答案:

答案 0 :(得分:0)

解决了!

例如,从对话框中选择第四项(“为此手机充电”):

val process = Runtime.getRuntime().exec(arrayOf("su", "-c", "system/bin/sh"))
val stream = DataOutputStream(process.outputStream)
stream.writeBytes("am start com.android.settings/.UsbSettings\n")
stream.writeBytes("input keyevent 21\n") // KEYCODE_DPAD_LEFT
stream.writeBytes("input keyevent 20\n") // KEYCODE_DPAD_DOWN
stream.writeBytes("input keyevent 20\n") // KEYCODE_DPAD_DOWN
stream.writeBytes("input keyevent 20\n") // KEYCODE_DPAD_DOWN
stream.writeBytes("input keyevent 66\n") // KEYCODE_ENTER
stream.flush()
stream.close()

(要选择第一项,只需用KEYCODE_DPAD_DOWN删除所有行)