[英]app layout wont draw until root permission is granted\denied
I'm a beginner android developer and trying to learn stuff while making a sample app. 我是一名初学者android开发人员,试图在制作示例应用程序时学习一些东西。
my MainActivity's onCreate() execute an AsyncTask that fetches data for my RecyclerView to hold. 我的MainActivity的onCreate()执行一个AsyncTask,该任务为我的RecyclerView保留数据。 the AsyncTask requires Root access and "asks" for it. AsyncTask需要Root访问权限并为其“请求”。
my problem is that the MainActivity's layout wont draw until the Root Prompt appears and i click grant\\deny. 我的问题是,直到出现根提示并单击Grant \\ deny,MainActivity的布局才会绘制。
how can i make the layout draw beforehand? 如何预先绘制布局?
Thanks in Advance!!! 提前致谢!!!
onCreate(): onCreate():
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
DataFetcher dataFetcher = new DataFetcher();
dataFetcher.execute("");
dataFetcher: 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;
}
将“ canRunRootCommands()”调用从onPreExecute移至doInBackground,以确保其未在UI线程上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.