点击按钮后,我想显示alertDialog
,显示animationDrawable
并执行asyncTask
。我发现当我将context
的{{1}}设置为getApplicationContext()时,应用程序崩溃了,当我将上下文设置为父类dialog
时,activity
当我在活动类中调用dismiss方法时,我被很好地解雇了;但是当我想在AsyncTask的onPostExecute中解除它时,对话框不会被解雇!这是代码:
dialog
那么如何使用上下文?
答案 0 :(得分:1)
设置Context in Constructor of AsyncTask
&传递AsyncTask调用的上下文。
Show your Dialogue in onPreExecute()
AsyncTask
Cancel
您的对话onPostExecute()
。
private class RunInBackground extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
private Context contextInsideDialogue;
public RunInBackground(Context activity) {
try {
this.contextInsideDialogue = activity;
dialog = new ProgressDialog(activity,
android.R.style.Theme_DeviceDefault_Light_Panel);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.getWindow().setGravity(Gravity.BOTTOM);
} catch (Exception e) {
}
}
@Override
protected void onPreExecute() {
try {
dialog.setMessage("Please wait. . ");
dialog.show();
} catch (Exception e) {
}
}
@Override
protected void onPostExecute(Void result) {
try {
if (dialog.isShowing()) {
dialog.dismiss();
}
} catch (Exception e) {
}
}
}
&安培;将其调用为
RunInBackground task = new RunInBackground (MyActivity.this);
task.execute();