以 root 身份运行本机 android 可执行文件?
有人知道如何做到这一点吗? 这是我的代码:
try {
Process p = Runtime.getRuntime().exec("su");
p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Process p = Runtime.getRuntime().exec(
"/system/bin/netcfg > /sdcard/netcfg.txt");
p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
是的,我知道我没有进行任何测试来查看用户是否确实接受了超级用户对话框,但这只是对我自己的测试。
Anybody knows how to to this?
This is my code:
try {
Process p = Runtime.getRuntime().exec("su");
p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Process p = Runtime.getRuntime().exec(
"/system/bin/netcfg > /sdcard/netcfg.txt");
p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Yes, I know I haven't got any tests to see if user actually accepted the superuser dialog, but this is just a test for myself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不会是您的完整解决方案,但这是我不久前尝试过的一些代码。它可以运行任意命令(代码在这里使用
ls
)。正如您从屏幕截图中看到的,ls
确实弹出了超级用户对话框。我最初想做的是使用
sh
打开 shell然后在该 shell 中运行命令,以便仅显示初始sh
超级用户对话框。我见过一些应用程序似乎可以做到这一点,但我仍然不确定它是如何完成的。我怀疑这也是您正在寻找的,但至少使用此代码您可以运行命令。 :)无论如何,这是代码。将其粘贴到 Android 应用程序框架中,它应该可以工作。
祝你好运!
This won't be your complete solution, but this is some code I was experimenting with a while ago. It CAN run an arbitrary command (the code uses
ls
here). As you can see from the screenshot, the superuser dialog does pop up forls
.What I had originally been trying to do was to open a shell with
sh
and then run commands within that shell so that only the initialsh
superuser dialog would every be shown. I've seen apps that seem to do this, but I'm still unsure how it is done. I suspect that is what you're looking for as well, but at least with this code you can run commands. :)Anyway, here's the code. Stick it into a skeleton Android app and it should work.
Good luck!
刚刚测试过,它无法在 Nexus S、操作系统 2.3.6 上运行。
更新
既然你的手机已经root,
su
应该可以工作。您看到的问题是密码对话框不显示。这似乎适用于某些已 root 的手机,但可能是自定义固件的功能。您可以做的是启动
su
进程,通过对话框询问用户密码,然后将其输出到su
进程。Just tested it and it does NOT work on Nexus S, OS 2.3.6.
Update
SInce your phone is rooted
su
should work. The problem that you are seeing is that password dialog does not show. This seems to work on some rooted phones, but is probably a feature of custom firmware.What you can do is start the
su
process, ask user for password via a Dialog and then output it tosu
process.我自己从未尝试过,但是如果您能够在没有 su 权限的应用程序中以 su 的方式执行操作,那么这没有多大意义。所以我想您需要做的第一件事就是为您的应用程序请求 su 权限:
然后您可以:(
复制自 http://forum.xda-developers.com/showpost.php?p=2954887&postcount=7)
I've never tried myself, but it wouldnt make a lot of sense for you to be able to do stuff as su from within an app without su rights. So I imagine the first thing you need to do is request su rights for your app:
Then you can:
(copied from http://forum.xda-developers.com/showpost.php?p=2954887&postcount=7)