我正在運行後臺線程。後臺線程成功執行後,我想在用戶界面上向用戶顯示一些警告消息。後臺線程成功執行後顯示AlertDialog
0
A
回答
2
如果您與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/
0
如果使用AsyncTask,那麼你可以編寫代碼顯示消息(也許吐司),在onPostExecute()
。
0
它不是不可能連接後臺線程與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()
0
啓動一個異步線程。異步線程爲您提供了三種方法:OnPreExecute(),doInBackground()和onPostExecute()。
在UI線程上調用第一個和最後一個方法,所以一旦在doInBackground中執行了操作,就會出現如下操作:
相關問題
- 1. 後臺線程完成後C#執行方法
- 2. GCM控制檯顯示成功後
- 3. 執行Javascript代碼在後臺線程
- 4. WindowsFormsSynchronizationContext.Current.Post後臺線程執行拉姆達
- 5. Spring和後臺線程執行
- 6. 在後臺執行計算(線程)
- 7. 執行在後臺和主線程ios
- 8. Android - 在ProgressDialog完成後顯示AlertDialog/Toast
- 9. 使用後臺線程顯示「Busy Indicator」
- 10. 後臺工作線程中的函數調用只有在主線程執行完成後才能執行?
- 11. ProgressDialog關閉後顯示alertDialog
- 12. AlertDialog後臺活動
- 13. 在後臺運行線程
- 14. 如何在服務器啓動後執行後臺線程?
- 15. 執行後臺線程後寫入NSOutputStream不起作用
- 16. 主線程在後臺線程執行時等待[NSManagedObjectContext(_NSInternalAdditions)lockObjectStore]
- 17. 後臺線程
- 18. 後臺線程
- 19. Android ASync任務ProgressDialog直到後臺線程完成後才顯示
- 20. 控件不顯示,如果運行在後臺線程(c#winform)
- 21. appcelerator - 在後臺執行功能?
- 22. iOS執行功能在後臺
- 23. wcf執行功能在後臺
- 24. 可以構建在後臺線程的形式,然後在UI線程顯示
- 25. 執行後顯示ProgressBar
- 26. 任何方式後面運行後臺線程多個活動並完成後顯示警報?
- 27. 在後臺執行
- 28. 當應用程序進入後臺時運行後臺線程
- 29. 使用ThreadPool完成其他線程後執行一些線程
- 30. 如何在所有線程完成執行後顯示消息框?