我想在我正在创建的Android应用程序中运行一些linux命令。我被告知,对于root访问权限,我需要在后台运行该命令以防止应用程序崩溃。我创建了这个asynctask方法:
private class Startup extends AsyncTask<Void, Void, Void> {
Process pr = null;
@Override
protected Void doInBackground(Void... params) {
// this method is executed in a background thread
// no problem calling su here
String cmdString = "su";
Runtime rt = Runtime.getRuntime();
try {
rt.exec(cmdString);
rt.exec("ip link set wlan0 address 00:80:48:BA:d1:30");
rt.exec("ip link set wlan0 broadcast 00:80:48:BA:d1:30");
pr = rt.exec("ip link show wlan0");
BufferedReader input = new BufferedReader(new InputStreamReader(
pr.getInputStream()));
String line = null;
while ((line = input.readLine()) != null)
{
Log.d("my log", "" + line);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
我想知道命令的输出是什么,但似乎每次我在代码中包含它(BufferReader)时,应用程序只是冻结而没有任何反应,所以我无法看到我的命令是否有效。如何检查运行命令的输出?还有,所有这些都应该在doInBackground()吗?