AsyncTask阻止UI线程

时间:2017-12-13 20:12:44

标签: android android-asynctask

我遇到AsyncTask问题,它会阻止用户界面并冻结屏幕

这是我的代码

new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... voids) {

                    AdServerManager.getInstance().getOfferClickCount(MultiOfferDetailsActivity.this, mCurrentOffer.getId(), ClickType.LIKE, new ClickViewCountUIListener() {
                        @Override
                        public void onClickViewCountCompleted(IncrementClickResponse response, AppError error) {
                            if (response != null)
                                mOfferLikes.setText(getString(R.string.likes2) + " " + PhoneUtils.decimalFormat(MultiOfferDetailsActivity.this, response.getCount()));
                            else
                                mOfferLikes.setText("");
                        }
                    });
                    return null;
                }
            }.execute();

这是服务器方法的代码

public void getOfferClickCount(final Context context, final int id, final ClickType clickType, final ClickViewCountUIListener uiListener) {

        if (BasePhoneUtils.isNetworkAvailable(context) == false) {

            uiListener.onClickViewCountCompleted(null, AppError.INTERNET_ERROR);

        } else {

            new AsyncTask<Void, Void, BaseResponse>() {

                @Override
                protected BaseResponse doInBackground(Void... sparams) {

                    mStartTime = System.nanoTime();

                    try {

                        String urlToCall = AdsConstants.ADS_SERVER_URL + AdsConstants.OFFER_GET_CLICK_COUNT;
                        String param = String.format(AdsConstants.OFFER_GET_CLICK_COUNT_PARAM_FORMAT, id, clickType.getValue());

                        Log.e("AdServerManager", urlToCall);
                        Log.e("AdServerManager", param);

                        BaseRequest request = new BaseRequest();
                        request.setURL(urlToCall);
                        request.setRequestType(RequestType.POST);
                        request.setStringParam(param);

                        mStopTime = System.nanoTime();
                        PerformanceManager.getInstance().showCalculatedTime("getOfferClickCount [doInBackground]", mStartTime, mStopTime);

                        return NetworkManager.execute(context, request, false, true);

                    } catch (Exception e) {
                        e.printStackTrace();
                        uiListener.onClickViewCountCompleted(null, AppError.DATA_ERROR);
                        return null;
                    }
                }

                protected void onPostExecute(BaseResponse result) {

                    mStartTime = System.nanoTime();

                    if (result != null && result.getResponse() != null) {
                        try {
                            uiListener.onClickViewCountCompleted(IncrementClickResponse.parseJSONObject(result.getResponse()), null);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            uiListener.onClickViewCountCompleted(null, AppError.PARSING_ERROR);
                        } catch (ParseException e) {
                            e.printStackTrace();
                            uiListener.onClickViewCountCompleted(null, AppError.PARSING_ERROR);
                        }
                    } else {
                        uiListener.onClickViewCountCompleted(null, AppError.GENERAL_ERROR);
                    }

                    mStopTime = System.nanoTime();
                    PerformanceManager.getInstance().showCalculatedTime("getOfferClickCount [onPostExecute]", mStartTime, mStopTime);
                }
            }.execute();
        }
    }

1 个答案:

答案 0 :(得分:1)

您似乎在尝试相互通信的同时激活了多个AsyncTasks。从Honeycomb开始,AsyncTask的默认执行程序是SERIAL_EXECUTOR,因此任何时候只能运行一个AsyncTask,并且您可能遇到死锁情况。您可以尝试使用THREAD_POOL_EXECUTOR执行任务作为可能的快速修复,即代替.execute()使用.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)