【问题标题】:asynctask freezes UI for time of threadasynctask 为线程时间冻结 UI
【发布时间】:2014-07-17 10:04:50
【问题描述】:

我正在为 Monitoring Cars App 做一个刷新按钮,所以当我点击它时,它会冻结 UI 几秒钟给定的线程,当我减少时间时,它不会正确获取数据(零或空数据)

点击监听器:

refreshcount.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            runOnUiThread(new Runnable() {

                @Override
                public synchronized void run() {
                    if (conDetector.isConnectingToInternet()) {
                        unitsRefreshTask = new AsyncRefreshUnit(
                                MonitoringController.usernamei,
                                MonitoringController.passwordi);
                        unitsRefreshTask.execute();
                        try {
                            Thread.sleep(5000);
                            //wait(11000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+" refreshcount.onclick() when calling AsyncRefreshUnit"+"\n"+e.getMessage().toString());
                        }

                    } else if (!conDetector.isConnectingToInternet()) {
                        showNoNet();
                    }
                }
            });     

            source = new CarsDataSource(getApplicationContext());
            source.open();

            MonitoringController.cars.clear();
            MonitoringController.cars = new ArrayList<Car>();
            ArrayList<Car> carList = new ArrayList<Car>();


            System.out.println("task.getResult().size(); >>"
                    + unitsRefreshTask.getResult().size());
            carList = (ArrayList<Car>) unitsRefreshTask.getResult();
            System.out.println("carList >>" + carList.size());


            for (int u = 0; u < carList.size(); u++) {
                System.out.println("carList.size() : >" + carList.size());
                MonitoringController.cars.add(carList.get(u));
                System.out.println("size"
                        + MonitoringController.cars.size());
            }

            for (int i = 0; i < carList.size(); i++) {
                SerialExist = source.isExistRoleAndSerial(getApplicationContext(),carList.get(i).getRoute_Nmae(),carList.get(i).getSerialNumber());
                if(SerialExist){
                    source.updateInfoByroleName(getApplicationContext(),carList.get(i));
                }else if(!SerialExist){
                    source.createCar(carList.get(i));
                }
                // System.out.println("update is >>" + update);
            }

            MonitoringController.countOfUnits = source.getCountsOfUnits(
                    getApplicationContext(), MonitoringController.routname);
            Log.e("C_InformationScreen.class",  source.getCountsOfUnits(
                    getApplicationContext(), MonitoringController.routname)+"");
            ((TextView) numOfUnits)
            .setText(source.getCountsOfUnits(
                    getApplicationContext(), MonitoringController.routname) + " Units");
            ((TextView) numOfUnits)
            .setText(carList.size() + " Units");



            source.close();
        }
    });

异步任务:

public class AsyncRefreshUnit extends
        AsyncTask<String, Void, ArrayList<Car>> {
    String user, pass, resultstring;
    ArrayList<Car> result = new ArrayList<Car>();
    int count;

    public List<Car> getResult() {
        return result;
    }

    public void setResult(ArrayList<Car> result) {
        this.result = result;
    }

    public AsyncRefreshUnit(String user, String pass) {
        super();
        this.user = user;
        this.pass = pass;
    }

    @Override
    protected void onPostExecute(ArrayList<Car> result) {
        super.onPostExecute(result);
        if(dialog.isShowing()){
            dialog.dismiss();
        }
            initList();
    }

    @SuppressLint("NewApi")
    private void finish(ArrayList<Car> result) {
        if (isCancelled()) {
            onCancelled(result);
        } else {
            onPostExecute(result);
        }
        Status mStatus = Status.FINISHED;
    }
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        dialog.setProgress(Integer.parseInt(progress[0]));

    }

    @Override
    protected void onPreExecute() {     
        dialog =  ProgressDialog.show(C_InformationScreen.this,"","Refreshing Units Data  ..");
        super.onPreExecute();

    }

    @Override
    protected ArrayList<Car> doInBackground(String... params) {
        try {
            httpClient = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(
                    InternetActions.getHttpParameters(110000), 70000); 
            HttpGet httpGet = new HttpGet(
                    "http://trackmonitor.net/software/AndroidControl.aspx?UserName="
                            + user + "&PassWord=" + pass + "&Action=Units");
            try {
                HttpResponse response = httpClient.execute(httpGet);
                int responsecode = response.getStatusLine().getStatusCode();
                if (responsecode == 200) {
                    InputStream in = response.getEntity().getContent();
                    resultstring = MonitoringController.convertinputStreamToString(in);
                    try {
                        JSONArray plates = new JSONArray(resultstring);
                        // MyAssmts.clear();
                        for (int i = 0; i < plates.length(); ++i) {
                            JSONObject jo = (JSONObject) plates.get(i);
                            result.add(convertToCar(jo));
                            Log.v("check on adding car_id to list", result.get(i)
                                    .getCarId() + "");
                            Log.v("check on adding car_name to list", result.get(i)
                                    .getName());
                            Log.v("check on adding car_serial to list",
                                    result.get(i).getSerialNumber());
                            Log.v("check on adding car_device to list",
                                    result.get(i).getDeviceId());
                            Log.v("check on adding car_phone to list", result
                                    .get(i).getPhone());
                            Log.v("check on adding car_role to list", result.get(i)
                                    .getRoute_Nmae());
                        }
                    } catch (JSONException  c) {
                        Log.v("Exception >>>", c.getMessage().toString());
                        c.printStackTrace();
                        MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground.JSONException "+"\n"+c.getMessage().toString());
                    }
                    long total = 0;
                    byte data[] = new byte[1024];
                    while ((count = in.read(data)) != -1) {
                        total += count;
                        // publishing the progress....
                        // After this onProgressUpdate will be called
                        setProgress((int)((total*100)/1000));
                    }
                }else if(responsecode != 200){
                    Toast.makeText(getApplicationContext(), "Your connection is break down please check your connection ..", Toast.LENGTH_LONG).show();
                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
                MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground.ClientProtocolException "+"\n"+e.getMessage().toString());
                Toast.makeText(getApplicationContext(), "Your connection is break down please check your connection ..", Toast.LENGTH_LONG).show();

            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Your connection is break down please check your connection ..", Toast.LENGTH_LONG).show();

                if (e.getMessage().indexOf("Connection reset by peer") > 0) {
                    Toast.makeText(getApplicationContext(),
                            "please Connect again ..", Toast.LENGTH_LONG)
                            .show();
                    MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground.IOException "+"\n"+e.getMessage().toString());

                }
            }               
            return result;
        } catch (Exception t) {
            t.printStackTrace();
            MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground.Exception "+"\n"+t.getMessage().toString());

        } finally {
            if (in != null) {
                try {
                    in.close();
                    if (httpClient != null)
                        httpClient.getConnectionManager().shutdown();
                } catch (IOException x) {
                    Log.e("Exception > ",  x.getMessage().toString());
                    MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"AsyncRefreshUnit.doInBackground when closing connection "+"\n"+x.getMessage().toString());

                }
            }
        }
        return null;
    }

【问题讨论】:

    标签: java android multithreading android-asynctask


    【解决方案1】:

    对“UI 冻结”的简单搜索揭示了您和其他许多人所犯的错误:

    不要在 UI 线程上睡觉或等待!!!

    为什么您甚至认为这样做是可以的?这就是 background 线程的用途。需要稍等片刻,然后用 UI 做点什么吗? 使用计时器! 需要在有延迟的循环中做某事? 使用计时器! 需要处理可能占用数百毫秒的大型任务? 使用后台任务!

    您所做的实际上是任何有关 UI 并发的教程都说不要做的第一件事。

    编辑

    在仔细查看您的代码之后,我对 sleep 到底应该做什么感到困惑。 您开始执行AsyncTask 之后,因此它不会真正延迟任何事情,除了重绘。你可能想把它放到后台线程来延迟执行或结果发布或smth。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 2015-03-17
      • 2011-06-22
      • 2021-12-21
      • 1970-01-01
      • 2021-03-10
      相关资源
      最近更新 更多