我是初学Android开发人员,在制作示例应用时尝试学习内容。
我的MainActivity的onCreate()执行一个AsyncTask,它为我的RecyclerView提取数据。 AsyncTask需要Root访问权限并为其“询问”。
我的问题是MainActivity的布局在根提示出现之前不会绘制,我点击grant \ deny。
如何预先绘制布局?
提前致谢!!!
的onCreate():
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
DataFetcher dataFetcher = new DataFetcher();
dataFetcher.execute("");
dataFetcher:
@Override
protected void onPreExecute() {
super.onPreExecute();
if(!canRunRootCommands()) {
Log.e("DataFetcher", "No Root Access");
cancel(true);
}
}
/***********************************************************************/
//Root Check method
//Credit: http://muzikant-android.blogspot.co.il/2011/02/how-to-get-root-access-and-execute.html
/***********************************************************************/
private boolean canRunRootCommands() {
boolean retval = false;
Process suProcess;
try {
suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes) {
// Getting the id of the current user to check if this is root
os.writeBytes("id\n");
os.flush();
String currUid = osRes.readLine();
boolean exitSu = false;
if (null == currUid) {
retval = false;
exitSu = false;
Log.d("ROOT", "Can't get root access or denied by user");
} else if (true == currUid.contains("uid=0")) {
retval = true;
exitSu = true;
Log.d("ROOT", "Root access granted");
} else {
retval = false;
exitSu = true;
Log.d("ROOT", "Root access rejected: " + currUid);
}
if (exitSu) {
os.writeBytes("exit\n");
os.flush();
}
}
} catch (Exception e) {
// Can't get root !
// Probably broken pipe exception on trying to write to output stream (os) after su failed, meaning that the device is not rooted
retval = false;
Log.d("ROOT", "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());
}
return retval;
}
答案 0 :(得分:0)
将onPreExecute的“canRunRootCommands()”调用移动到doInBackground,以确保它不在UI线程上运行。