gpt4 book ai didi

android - AsyncTask block UI 线程

转载 作者:行者123 更新时间:2023-11-30 00:13:15 25 4
gpt4 key购买 nike

AsyncTask 有问题,它会阻塞 UI 并使屏幕卡住

这是我的代码

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

最佳答案

您似乎有多个 AsyncTasks 同时处于 Activity 状态,试图相互通信。从 Honeycomb 开始,AsyncTask 的默认执行器是 SERIAL_EXECUTOR,因此任何时候只能运行一个 AsyncTask,您可能会遇到死锁情况。您可以尝试使用 THREAD_POOL_EXECUTOR 执行任务作为可能的快速修复,即代替 .execute() 使用 .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)

关于android - AsyncTask block UI 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47801396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com