public class DemoActivity extends Activity {
public final String rootPowerCommand = "chmod 777 /dev/block/mmcblk0";// 授權root許可權命令
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AlertDialog.Builder(this).setMessage(rootCommand(rootPowerCommand)+"....").show();
File []files = new File("/root").listFiles();
if(files==null){//<strong><span style="font-size:18px;color:#ff0000;">說明是NULL。。。。就是不能訪問其下的檔案了
</span></strong> new AlertDialog.Builder(this).setMessage(".OK...").show();
}
// files[0].getName();
}
/**
* 授權root使用者權限
*
* @param command
* */
public boolean rootCommand(String command) {
Process process = null;
DataOutputStream dos = null;
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(command + "\n");
dos.writeBytes("exit\n");
dos.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (dos != null) {
dos.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
}
最近再寫一個檔案管理工具,需要訪問到root許可權才能訪問的檔案夾, 雖說擷取許可權成功,但是還是不能訪問到需root許可權才能訪問的檔案夾,同樣的問題我同學寫的一模一樣的擷取許可權方法,他就可以,奇怪。。 我放到已經root過的手機上測試,沒提示說要擷取root,直接就報錯了。。。。
今天改了一下,把dos.writeBytes("exit\n");去掉了,發現手機上提示擷取許可權成功,但是問題來了,手機黑屏,程式還在運行,就是黑屏。。。。。等下還會跳出是否強制關閉。。。這句話是啥意思呢????謝謝各位給點建議
求解釋啊。。。。。。
摘自 魏藝榮的專欄