我想知道如何在此类中实现线程以使其免受ANR(应用程序无响应)问题的影响
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; } } }
例如,如果当我按下一个按钮时此代码被执行,如果我多次按下此按钮,则我的应用程序具有ANR.
解决方法:
您不想使用线程,而是要使用AsyncTask.这是如何做:
根据以下内容,确定您的应用程序需要什么:AsyncTask< TypeOfVarargParams,ProgressValue,ResultValue>
一些启发:
public class MyClass { //Something public MyClass() { new BackgroundTask().execute("Hello World"); }}private class BackgroundTask extends AsyncTask<String, VoID, String> { @OverrIDe protected voID onPreExecute() { // Prepare your background task. This will be executed before doInBackground } @OverrIDe protected String doInBackground(String... params) { // Your main code goes here String iAmAString = "I have done something very heavy Now..."; return iAmAString; } @OverrIDe protected voID onPostExecute(String result) { // Whatever should happen after the background task has completed goes here } @OverrIDe protected voID onProgressUpdate(VoID... values) { // In here, you can send updates to you UI thread, for example if you're downloading a very large file. }}
总结 以上是内存溢出为你收集整理的java-在我的课堂上实现Thread Android吗?全部内容,希望文章能够帮你解决java-在我的课堂上实现Thread Android吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)