Android 进程对话框导致停止线程时出现问题
我有两个按钮:一个用于启动函数的执行,如下所示,带有线程和进度对话框Android 进程对话框导致停止线程时出现问题,android,Android,我有两个按钮:一个用于启动函数的执行,如下所示,带有线程和进度对话框 start_button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.d(TAG, "mMainButton clicked"); showLoading();
start_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "mMainButton clicked");
showLoading();
// Calling showdialog before downloading..
Thread t = new Thread(){
public void run() {
startdownload();
// Code for download....
// After download UI thread will call
runOnUiThread(new Runnable() {
public void run() {
dialog.cancel();
alertbox("Info","Download Completed");
}
});
}};
t.start();
}
});
public ProgressDialog dialog;
public void showLoading () {
// prepare the dialog box
//ProgressDialog
dialog = new ProgressDialog(this);
// make the progress bar cancelable
dialog.setCancelable(true);
// set a message text
dialog.setMessage("Please wait while Downloading..");
// show it
dialog.show();
}
protected void alertbox(String title, String mymessage) {
new AlertDialog.Builder(this)
.setMessage(mymessage)
.setTitle(title)
.setCancelable(true)
.setNeutralButton("OK",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton){}
}) .show();
}
public Boolean startdownload() {
try
{
downloadhelper = new download_helper(this);
downloadhelper.DownloadProblemAndReasonCode();
pkManifest=downloadhelper.DownloadNewManifest();
downloadhelper.DownloadNewMessages();
//downloadhelper.DeleteOldManifest();
}
catch(Exception exception)
{
exception.toString();
}
return false;
}
以及另一个停止下载的按钮,如下所示:
stop_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onStop();
MessageBox("Down Load Stopped");
}
});
public void onStop() {
super.onStop();
isRunning = false;
}
问题是,由于前面的进度对话框,我无法单击按钮停止启动的功能
有没有办法停止执行单击的开始按钮功能?在我看来,正确的解决方案是启用进度对话框(由ProgressDialog自动提供)的取消按钮功能这对于更为android一致的界面也有意义。在我看来,正确的解决方案是启用进度对话框的取消按钮功能(由ProgressDialog自动提供)。这对于更为android一致的界面也有意义。您可以设置进度对话框的oncancellistner
ProgressDialog p;
p.setCancelable(true);
p.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//stop thread execution
}
});
如果用户取消对话框,这将取消执行。(后退键)
编辑
在回答阿伦的问题时
我没有测试过这个
ProgressDialog p;
p.setButton(ProgressDialog.BUTTON_NEGATIVE, "Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//This might work
}
});
您可以设置进度对话框的oncancellistner
ProgressDialog p;
p.setCancelable(true);
p.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//stop thread execution
}
});
如果用户取消对话框,这将取消执行。(后退键)
编辑
在回答阿伦的问题时
我没有测试过这个
ProgressDialog p;
p.setButton(ProgressDialog.BUTTON_NEGATIVE, "Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//This might work
}
});
使用Asynctask显示进度对话框并将其关闭。在该对话框中,在OnPreExecute()中显示进度对话框,并在OnPostExecute()中关闭对话框。请参阅此链接:
使用Asynctask显示进度对话框并关闭它。在该对话框中,在OnPreExecute()中显示进度对话框,并在OnPostExecute()中关闭对话框。请参阅此链接:
我启用了该对话框。可设置可取消(true);但是在程序中没有找到任何取消按钮dialog@Arun-在调用-show-之前需要执行此操作-是这样吗?我启用了对话框。setCancelable(true);但是在程序中没有找到任何取消按钮dialog@Arun-您需要在调用-show之前执行此操作-是这样吗?有没有办法在progrss中添加一个按钮dialog@Arun:更新了我的答案,还有其他重载setButton()有没有办法在progrss中添加一个按钮dialog@Arun:更新了我的答案,还有其他重载setButton()