后台线程成功执行后显示AlertDialog
答
如果您与AsynTask
合作,那么您可以在onPostExecute()
中显示它。 http://www.mysamplecode.com/2011/09/android-asynctask-httpclient-with.html
AlertDialog alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
更多的帮助与警报看到http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/
答
它不是不可能连接后台线程与UI.With处理程序的帮助下,你可以发送messages.By检查消息,你可以显示alert.i认为这段代码将帮助你。
Thread animator = new Thread() {
public void run() {
int i = 0;
try {
sleep(4000);
while (i < 4) {
sleep(50);
handler.sendMessage(handler.obtainMessage(i));
i++;
}
} catch (Exception e) {
}
}
};
animator.start();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
animatedimage.setImageResource(R.drawable.sub1);
} else if (msg.what == 1) {
animatedimage.setImageResource(R.drawable.sub2);
} else if (msg.what == 2) {
animatedimage.setImageResource(R.drawable.sub3);
} else if (msg.what == 3) {
animatedimage.setImageResource(R.drawable.sub4);
}
}
};
如果您使用的是Assync tasy你能做到在
onPostExecute()
答
启动一个异步线程。异步线程为您提供了三种方法:OnPreExecute(),doInBackground()和onPostExecute()。
在UI线程上调用第一个和最后一个方法,所以一旦在doInBackground中执行了操作,就会出现如下操作: