实现线程的Andr​​oid在我的课? [英] Implement Thread Android in my class?

查看:128
本文介绍了实现线程的Andr​​oid在我的课?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何实现一个线程在这个类,使其从ANR的问题(应用程序无响应)

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;
        }
    }


        }

例如,如果该code是执行时,我preSS一个按钮,如果我preSS很多次这个按钮,我的应用程序有一个ANR。

If for example this code is execute when i press a button, if i press many times this button, my app have an ANR.

推荐答案

您不想使用,而在的AsyncTask 。具体方法如下:

You don't want to use a Thread, but an AsyncTask. Here's how:

根据以下内容,找出你需要为你的应用程序:的AsyncTask< TypeOfVarArgParams,ProgressValue,ResultValue>

Based on the following, figure out what you need for your app: 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.
      }
}   

这篇关于实现线程的Andr​​oid在我的课?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆