gpt4 book ai didi

android - Android中AsyncTask启动但不处理

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

我知道已经有很多类似的 AsyncTask 问题,但在我的情况下有些东西很不寻常或者我错过了什么!?

因为 AsyncTask 不允许运行多次。我用 new B().execute(); 调用它,所以它应该在每次运行时创建一个单独的实例!?对吧?

问题是B类创建并执行一次后,用户第二次调用startBClass()方法时就不起作用了(只是打开了对话框,实际工作并没有发生).

刚才Debug了代码,发现Dialog关闭后,Thread还在后台运行。 Dialog 关闭时停止后台线程的正确方法是什么? - 由于我正在关闭 B 类中的第一个 Dialog 并创建 B 类的另一个实例,为什么第二个不工作?多个 AsyncTasks 不能并行运行!?

我简化了类以便于理解我正在尝试的内容:

public class A {

/* Is called when user clicks a button */
private void startBClass() {
new B().execute();
}

/* Opens a Dialog with a countdown TextView (works first call only) */
private class B extends AsyncTask<Void, Integer, Void> {

private int secondsPassed = 0;
private double totalToPay = 0;

private Dialog dialog;
private TextView tvCost;
private Button dialogBtn;

@Override
protected void onPreExecute() {
super.onPreExecute();

dialog = new Dialog(ConfigurationActivity.this);
dialog.setCancelable(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
dialog.setCanceledOnTouchOutside(false);

dialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
onPostExecute(null);
}
});

tvCost = (TextView) dialog.findViewById(R.id.textCounter);
dialogBtn = (Button) dialog.findViewById(R.id.button1);
dialogBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});

dialog.show();
}

@Override
protected Void doInBackground(Void... arg0) {
while(true){
publishProgress(secondsPassed++);
SystemClock.sleep(1000);
}
}

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
totalToPay = 12.00;
tvCost.setText(totalToPay + " USD");
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
final AlertDialog alert = new AlertDialog.Builder(ConfigurationActivity.this).create();
alert.setTitle("Information");
alert.setMessage("You should pay about " + totalToPay + " USD.");
alert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface alertDialog, int which) {
alertDialog.dismiss();
}
});

alert.show();
}
}
}

最佳答案

dialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
onPostExecute(null);
}
});

这样不好。每the docs :

Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.

为了结束它,我将更改上面的代码和 doInBackground() 中的 while 循环。

   protected Void doInBackground(Void... arg0) {
while(running){
publishProgress(secondsPassed++);
SystemClock.sleep(1000);
}
}

running 是您在 onPreExecute() 中设置为 true 的 bool 值。当你想结束它时,将它设置为 false。然后你的循环将退出并且 onPostExecute() 将被正确调用。

旁注:secondsPassed 在哪里使用过?

关于android - Android中AsyncTask启动但不处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15126321/

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