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");去掉瞭,發現手機上提示獲取權限成功,但是問題來瞭,手機黑屏,程序還在運行,就是黑屏。。。。。等下還會跳出是否強制關閉。。。這句話是啥意思呢????謝謝各位給點建議
求解釋啊。。。。。。
摘自 魏藝榮的專欄