以root用户身份运行本机android可执行文件?
任何人都知道如何去做到这一点? 这是我的代码:以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();
}
是的,我知道我还没有得到任何测试,看看用户实际接受了超级用户对话,但是这仅仅是一个测试自己。
我从来没有尝试过自己,但它wouldnt使很多的感觉让你能够从一个应用程序中做的东西,苏不苏权利。所以,我想你需要做的第一件事就是为你的应用程序的请求苏权利:
// arbitrary number of your choosing
final int SUPERUSER_REQUEST = 2323;
// superuser request
Intent intent = new Intent("android.intent.action.superuser");
// tell Superuser the name of the requesting app
intent.putExtra("name", "Shell");
// tel Superuser the name of the requesting package
intent.putExtra("packagename", "koushikdutta.shell");
// make the request!
startActivityForResult(intent, SUPERUSER_REQUEST);
然后,您可以:
Runtime.getRuntime().exec("su -c <your privileged command here>");
(从http://forum.xda-developers.com/showpost.php?p=2954887&postcount=7复制)
我会尝试,谢谢:-) –
您的文章+ http ://matejcik.blogspot.com/2010/10/how-to-correctly-execute-su-from.html为我工作;) –
错误。应用程序可以执行“su”,此时超级用户应用程序要求用户有权以超级用户模式运行应用程序的命令。无需自己运行超级用户活动。 –
经过测试,它不适用于Nexus S OS 2.3.6。
更新
,因为你的手机是植根su
应该工作。您看到的问题是密码对话框不显示。这似乎适用于某些固定电话,但可能是自定义固件的一项功能。
你可以做的是启动su
进程,通过对话框询问用户密码,然后输出到su
进程。
Process process = new ProcessBuilder()
.command("su", "android.com")
.redirectErrorStream(true)
.start();
// Dialog for asking pass here
OutputStream out = process.getOutputStream();
out.write(password);
我知道,这就是为什么我问如何去做。 –
AFAIK你不能(在普通的非根深蒂固的手机上)。这将违反安全原则:http://developer.android.com/guide/topics/security/security.html –
我知道,我扎根;) –
这会不会是你的完整解决方案,但这是我刚刚尝试的一些代码。它可以运行任意命令(代码在这里使用ls
)。从屏幕截图中可以看到,超级用户对话框会弹出ls
。
我原本一直在努力做的是打开外壳,sh
然后该壳中运行命令,以便将每一个显示只有初始sh
超级用户对话框。我看过似乎这样做的应用,但我仍然不确定它是如何完成的。我怀疑这也是你在寻找的,但至少在这个代码中你可以运行命令。 :)
无论如何,这是代码。将其粘贴到一个骷髅Android应用程序,它应该工作。
public class ShellCommandTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final StringBuilder log = new StringBuilder();
try{
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("su");
commandLine.add("-c");
commandLine.add("ls");
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
for (String command : commandLine){
Log.i("SU_Test", command);
}
String line;
while ((line = bufferedReader.readLine()) != null){
log.append(line);
log.append("\n");
}
}
catch (IOException e){
Log.e("SU_Test", "Fail: " + e.getMessage());
}
Log.i("SU_Test", log.toString());
}
祝你好运!
真棒,谢谢!将尝试;) –
你自己测试一下吗? – MasterCassim
@MasterCassim我自己测试过,我在问怎么做,因为它不工作。 –