Asynctask 和 onclicklistener android 2.3.3 与 3.2
在我的 Nexus One (android 2.3.3) 上,以下程序的行为与我的 xoom (android 3.2) 不同:
public class TestOnclickWithAsyncTask extends Activity {
private int mClicked = 0;
private final static String TAG = "TestOnclickWithAsyncTask";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.img_control_panel);
final Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mClicked++;
Log.i (TAG, "Clicked "+ mClicked + " times");
}
});
relativeLayout.addView(button);
new LongTask().execute();
}
private class LongTask extends AsyncTask<Void , Void, Void> {
private ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(TestOnclickWithAsyncTask.this);
dialog.setMessage("Waiting ");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
try {
dialog.show();
} catch (Exception e){}
}
protected Void doInBackground(Void... notused) {
try {
Thread.sleep (30000);
} catch (InterruptedException e) {
e.printStackTrace();
}//do long task
return null;
}
protected void onPostExecute(Void unusedg) {
dialog.dismiss();
Log.i (TAG,"Long task finished");
}
}
}
在我的 Nexus 上,我无法在 asynctask 运行时按下按钮(所需的行为):
09-26 00:45:34.361: INFO/TestOnclickWithAsyncTask(3803): Long task finished
09-26 00:45:36.463: INFO/TestOnclickWithAsyncTask(3803): Clicked 1 times
09-26 00:45:36.814: INFO/TestOnclickWithAsyncTask(3803): Clicked 2 times
09-26 00:45:37.975: INFO/TestOnclickWithAsyncTask(3803): Clicked 3 times
09-26 00:45:38.435: INFO/TestOnclickWithAsyncTask(3803): Clicked 4 times
在我的 xoom 上,我只需按按钮和进度指示器消失。
09-26 00:49:05.440: INFO/TestOnclickWithAsyncTask(5887): Clicked 1 times
09-26 00:49:06.230: INFO/TestOnclickWithAsyncTask(5887): Clicked 2 times
09-26 00:49:06.580: INFO/TestOnclickWithAsyncTask(5887): Clicked 3 times
09-26 00:49:06.840: INFO/TestOnclickWithAsyncTask(5887): Clicked 4 times
09-26 00:49:08.170: INFO/TestOnclickWithAsyncTask(5887): Clicked 5 times
09-26 00:49:08.560: INFO/TestOnclickWithAsyncTask(5887): Clicked 6 times
09-26 00:49:30.960: INFO/TestOnclickWithAsyncTask(5887): Long task finished
09-26 00:49:32.800: INFO/TestOnclickWithAsyncTask(5887): Clicked 7 times
09-26 00:49:33.810: INFO/TestOnclickWithAsyncTask(5887): Clicked 8 times
为什么会有所不同?更重要的是,如何防止 xoom 取消进度对话框?
On my nexus One (android 2.3.3) the following program behaves differently from my xoom (android 3.2):
public class TestOnclickWithAsyncTask extends Activity {
private int mClicked = 0;
private final static String TAG = "TestOnclickWithAsyncTask";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.img_control_panel);
final Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mClicked++;
Log.i (TAG, "Clicked "+ mClicked + " times");
}
});
relativeLayout.addView(button);
new LongTask().execute();
}
private class LongTask extends AsyncTask<Void , Void, Void> {
private ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(TestOnclickWithAsyncTask.this);
dialog.setMessage("Waiting ");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
try {
dialog.show();
} catch (Exception e){}
}
protected Void doInBackground(Void... notused) {
try {
Thread.sleep (30000);
} catch (InterruptedException e) {
e.printStackTrace();
}//do long task
return null;
}
protected void onPostExecute(Void unusedg) {
dialog.dismiss();
Log.i (TAG,"Long task finished");
}
}
}
On my Nexus I can't press the button while the asynctask is running (desired behavior):
09-26 00:45:34.361: INFO/TestOnclickWithAsyncTask(3803): Long task finished
09-26 00:45:36.463: INFO/TestOnclickWithAsyncTask(3803): Clicked 1 times
09-26 00:45:36.814: INFO/TestOnclickWithAsyncTask(3803): Clicked 2 times
09-26 00:45:37.975: INFO/TestOnclickWithAsyncTask(3803): Clicked 3 times
09-26 00:45:38.435: INFO/TestOnclickWithAsyncTask(3803): Clicked 4 times
On my xoom I can just press the button and the progress indicator disappears.
09-26 00:49:05.440: INFO/TestOnclickWithAsyncTask(5887): Clicked 1 times
09-26 00:49:06.230: INFO/TestOnclickWithAsyncTask(5887): Clicked 2 times
09-26 00:49:06.580: INFO/TestOnclickWithAsyncTask(5887): Clicked 3 times
09-26 00:49:06.840: INFO/TestOnclickWithAsyncTask(5887): Clicked 4 times
09-26 00:49:08.170: INFO/TestOnclickWithAsyncTask(5887): Clicked 5 times
09-26 00:49:08.560: INFO/TestOnclickWithAsyncTask(5887): Clicked 6 times
09-26 00:49:30.960: INFO/TestOnclickWithAsyncTask(5887): Long task finished
09-26 00:49:32.800: INFO/TestOnclickWithAsyncTask(5887): Clicked 7 times
09-26 00:49:33.810: INFO/TestOnclickWithAsyncTask(5887): Clicked 8 times
Why does this differ? And more importantly how can I prevent the xoom from canceling the progress dialog?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您不希望这样时,为什么还要调用
setCancelable(true)
呢?也许这会解决您的问题...只是一个提示:
try ... catch
中,并使用一般异常捕获而不做出任何反应,这是一个非常糟糕的习惯。当您这样做时,您应该始终留下评论并给出充分的理由。Why do you call
setCancelable(true)
when you don't want it to be that way? Maybe this will solve your issue...Just a hint:
try ... catch
with a general exception catch without a reaction is a really bad habit. When you do that you should always leave a comment with a good reason for that.