Java 如何在运行的线程中显示警报对话框?
我想根据属性显示一个警报对话框,当用户单击OK按钮时,再次调用该函数以获取运行进程中的更新值 我有以下代码:Java 如何在运行的线程中显示警报对话框?,java,android,android-layout,android-widget,Java,Android,Android Layout,Android Widget,我想根据属性显示一个警报对话框,当用户单击OK按钮时,再次调用该函数以获取运行进程中的更新值 我有以下代码: importingProgress=ProgressDialog.show(上下文,getString(R.string.progressNewsListTitle)), getString(R.string.progressProjectListMessage),true); 新线程(newrunnable()){ 公开募捐{ 试一试{ app.SetOtherTaskRunning(
importingProgress=ProgressDialog.show(上下文,getString(R.string.progressNewsListTitle)),
getString(R.string.progressProjectListMessage),true);
新线程(newrunnable()){
公开募捐{
试一试{
app.SetOtherTaskRunning(true);
Ib_clients client=db.Ib_clients_GetById(app.GetCustomerId());
试一试{
LogManager.WriteToFile(“--------------从Web服务获取新闻:-”+DateFormat.getDateTimeInstance().format(new Date())+“--------------”;
CommonFunctions.CreateXml(上下文、h、客户端、db、app.GetBookMonth()、app.GetBookQuater()、app.GetBookYear()、常量.News、app.GetWebServiceLastSyncDate(常量.ServiceType.News.toString())、常量.ServiceType.News、null、null、null、null);
Return-response=null;
做{
response=CommonFuctions.SendingRequest(上下文、处理程序、数据库);
if(response.type.compareTo(“警告”)==0){
h、 post(新的Runnable(){
公开募捐{
AlertDialog.Builder alert=新建AlertDialog.Builder(上下文);
setTitle(context.getString(R.string.information));
alert.setMessage(“dsgdgd”);
alert.setPositiveButton(context.getString(R.string.logoutDialogOk),new DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
}
});
alert.show();
}
});
}
}while(response.type.compareTo(“警告”)==0);
}捕获(IOE异常){
e、 printStackTrace();
}
}捕获(例外e){
//Log.d(Constants.TAG,e.getMessage());
e、 printStackTrace();
}
if(导入进度!=null){
importingProgress.disclose();
importingProgress=null;
}
}
}).start();
如果响应类型为警告
,则向用户显示一条消息,如果用户单击确定
按钮,则再次调用CommonFunctions.SendingRequest(上下文、处理程序、数据库)
,以获取更新的值。在得到.response类型的警告之前,我们需要向用户显示一个警报对话框,并再次调用CommonFuctions.SendingRequest(上下文、处理程序、数据库)
要返回的类:
公共类返回{
公共字符串类型;
公共字符串msg;
公共访问;
public int client_id;//用于从服务器获取clientid
public int booking_id;//用于从服务器获取bookingid
}
您必须使用handler来显示AlertDialog,因为UI只能由主线程处理
另一种方法是使用asyncTask进行多处理,然后使用asyncTask的onPostExcecute()显示AlertDialog
请随时询问任何进一步的疑问。尝试在runonUIthread中运行以下对话框:
将alertdialog代码与主线程分开。请尝试在runonUIThread上运行它。您可以使用处理程序及其post方法来执行此操作,因为它必须在UI线程上运行。请不要在代码中使用制表符。当其他人编辑你的问题时,它们可能会中断。谢谢我也可以使用handler.post()方法来显示警报对话框,但我希望在用户未单击警报对话框中的“确定”按钮时停止循环,否则循环将继续运行,而不会在警报对话框中停止。@SushantBhatnagar我仍然面临一个问题。
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle(context.getString(R.string.information));
alert.setMessage("dsgdgd");
alert.setPositiveButton(context.getString(R.string.logoutDialogOk), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
});