Asynctask and onclicklistener android 2.3.3 versus 3.2
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?
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 {
dialog.show();
} catch (Exception e) {
}
- Why are you doing that?
- Enclosing code in
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.
精彩评论