我想从我的onCreateView
方法中启动另一个线程,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable(){
public void run(){
checkRoot();
}
}).start();
}
但我在 logcat 中收到此错误:
06-16 12:52:37.088: E/AndroidRuntime(9707): FATAL EXCEPTION: Thread-804
06-16 12:52:37.088: E/AndroidRuntime(9707): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
我知道我可以使用AsyncTask
,但我想使用上述方法只是出于好奇。
这是checkRoot
方法:
private void checkRoot(){
Process p;
try{
// Preform su to get root privledges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("mount -o rw,remount -t yaffs2 /dev/block/mtdblock0 /system\n");
os.writeBytes("echo \"Do I have root?\" >/system/etc/temporary.txt\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
try{
p.waitFor();
if(p.exitValue() != 225){
showToast("ROOTED !");
} else {
showToast("not root");
setContentView(R.layout.no_root);
}
} catch(InterruptedException e){
showToast("not root");
setContentView(R.layout.no_root);
}
} catch(IOException e){
showToast("not root");
setContentView(R.layout.no_root);
}
}