I would like to know how to implement a thread in this class to make it safe from the problems of ANR (Application Not Responding)
public class myClass {
private static String LOG_TAG = Root.class.getName();
public boolean isDeviceRooted() throws IOException {
if (checkRootMethod1()){return true;}
if (checkRootMethod2()){return true;}
if (checkRootMethod3()){return true;}
return false;
}
public boolean checkRootMethod1(){
String buildTags = android.os.Build.TAGS;
if (buildTags != null && buildTags.contains("test-keys")) {
return true;
}
return false;
}
public boolean checkRootMethod2(){
try {
File file = new File("/system/app/Superuser.apk");
if (file.exists()) {
return true;
}
else {
return false;
}
} catch (Exception e) {
}
return false;
}
public boolean checkRootMethod3() {
if (new ExecShell().executeCommand(SHELL_CMD.check_su_binary) != null){
return true;
}else{
return false;
}
}
}
If for example this code is execute when i press a button, if i press many times this button, my app have an ANR.
You don't want to use a
Thread
, but anAsyncTask
. Here's how:Based on the following, figure out what you need for your app:
AsyncTask<TypeOfVarArgParams, ProgressValue, ResultValue>
Some inspiration: