我正在运行一个AsyncTask
,它需要一些时间来加载。在那段时间里,如果我按回按钮,那么它就不会回应。它只在几秒钟后响应。那么如何杀死或暂停或覆盖AsyncTask
回去?或者有没有其他方法可以做类似的事情?当按下后退按钮时,如何停止android中的asynctask?
if (mainContent != null) {
mainContent.post(new Runnable() {
@Override
public void run() {
Bitmap bmp = Utilities.getBitmapFromView(mainContent);
BlurFilter blurFilter = new BlurFilter();
Bitmap blurredBitmap = blurFilter.fastblur(bmp,1,65);
asyncTask = new ConvertViews(blurredBitmap);
asyncTask.execute();
}
});
我AsyncTask
:
class ConvertViews extends AsyncTask<Void,Void,Void> {
private Bitmap bmp;
public ConvertViews(Bitmap bmp){
this.bmp = bmp;
}
@Override
protected Void doInBackground(Void... params) {
try {
//Thread.sleep(200);
if(mainViewDrawable == null) {
mainViewDrawable = new BitmapDrawable(getResources(), bmp);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我onBackPressed()
:
public void onBackPressed() {
super.onBackPressed();
asyncTask.cancel(true);
finish();
}
发布您的代码? –
我是这个平台的新手。你能告诉我,我如何发布代码?它在发布时显示错误。 –
将您的代码复制并粘贴到编辑框中,在编辑框中选择所有代码,然后按Ctrl + K进行格式化。然后提交它。 –