在设定的时间后删除对话框
Removing a dialog after a set amount of time
public void isWaitingResponse (boolean isWaiting) {
if (isWaiting && dialogLock == null) {
dialogLock = new Dialog(this, android.R.style.Theme_Panel);
dialogLock.setCancelable(false);
dialogLock.show();
// ToDo: If this dialog is still showing after 10 seconds
// Call the primary method within and activate the "else"
// condition code below to remove dialog, and clear app
// state for further communication
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (isWaiting && dialogLock != null) {
// Recursively call this method as false
isWaitingResponse(false);
}
}
}, 5000);
} else if (!isWaiting) {
if (dialogLock != null) {
dialogLock.dismiss();
dialogLock = null;
}
}
}
如上面代码中的注释中所述,这确实有效,并且可以正确完成我需要的操作。我只是想知道我是否要附加处理程序,或者是否需要 任何额外的清理 以确保活动代码中没有遗留垃圾。对处理程序不是很熟悉。
更新:
我想确保如果对话框关闭(此方法使用错误参数输入),此处理程序不会触发。因此,在错误检查时,如果处理程序有 post 延迟,则该处理程序应与对话框一起关闭。我必须确保以后的请求不会被前一个请求的 post 延迟超时拒绝。
已解决,下面是我自己发布的答案 - 感谢 Yessine。
显示对话框后输入此代码,它将在 10 秒后 运行
new Timer().schedule(new TimerTask() {
public void run() {
dialogLock.dismiss();
}}, 10000);//time in milliseconds
这个答案是@Yessine Mahdouani 建议的实现。
首先创建一个定时器:
final Timer timerUnlock = new Timer();
锁定进程时还涉及安排一个计时器事件,该事件会重新启动根方法本身:
if (isWaiting &&
lockDialog == null)
{
lockDialog = new Dialog(this, android.R.style.Theme_Panel);
lockDialog.setCancelable(false);
lockDialog.show();
timerUnlock.schedule(new TimerTask() {
@Override
public void run() {
// Close it automatically if running.
m_setisWaiting(false);
}
}, 10000);
}
解锁时确保取消任何计时器,这可以防止先前的锁定计时器解锁未完成且尚未超时的更新锁:
else if (!isWaiting)
{
if (lockDialog != null) {
lockDialog.dismiss();
lockDialog = null;
}
timerUnlock.cancel();
}
最后,将在接口级别上运行的代码应该在主线程上 运行(否则会导致连续崩溃 - 可能是因为计时器正在完成并从单独的线程触发:
runOnUiThread(new Runnable() {
@Override
public void run() {
// Remove any timeouts still running.
if(progressBar != null) {
if(isWaiting)
progressBar.setVisibility(View.VISIBLE);
else
progressBar.setVisibility(View.GONE);
}
}
});
完整方法如下:
public void m_setisWaiting (boolean isWaiting) {
final Timer timerUnlock = new Timer();
if (isWaiting &&
lockDialog == null)
{
lockDialog = new Dialog(this, android.R.style.Theme_Panel);
lockDialog.setCancelable(false);
lockDialog.show();
timerUnlock.schedule(new TimerTask() {
@Override
public void run() {
// Close it automatically if running.
m_setisWaiting(false);
}
}, 10000);
}
else if (!isWaiting)
{
if (lockDialog != null) {
lockDialog.dismiss();
lockDialog = null;
}
timerUnlock.cancel();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// Remove any timeouts still running.
if(progressBar != null) {
if(isWaiting)
progressBar.setVisibility(View.VISIBLE);
else
progressBar.setVisibility(View.GONE);
}
}
});
}
public void isWaitingResponse (boolean isWaiting) {
if (isWaiting && dialogLock == null) {
dialogLock = new Dialog(this, android.R.style.Theme_Panel);
dialogLock.setCancelable(false);
dialogLock.show();
// ToDo: If this dialog is still showing after 10 seconds
// Call the primary method within and activate the "else"
// condition code below to remove dialog, and clear app
// state for further communication
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (isWaiting && dialogLock != null) {
// Recursively call this method as false
isWaitingResponse(false);
}
}
}, 5000);
} else if (!isWaiting) {
if (dialogLock != null) {
dialogLock.dismiss();
dialogLock = null;
}
}
}
如上面代码中的注释中所述,这确实有效,并且可以正确完成我需要的操作。我只是想知道我是否要附加处理程序,或者是否需要 任何额外的清理 以确保活动代码中没有遗留垃圾。对处理程序不是很熟悉。
更新: 我想确保如果对话框关闭(此方法使用错误参数输入),此处理程序不会触发。因此,在错误检查时,如果处理程序有 post 延迟,则该处理程序应与对话框一起关闭。我必须确保以后的请求不会被前一个请求的 post 延迟超时拒绝。
已解决,下面是我自己发布的答案 - 感谢 Yessine。
显示对话框后输入此代码,它将在 10 秒后 运行
new Timer().schedule(new TimerTask() {
public void run() {
dialogLock.dismiss();
}}, 10000);//time in milliseconds
这个答案是@Yessine Mahdouani 建议的实现。
首先创建一个定时器:
final Timer timerUnlock = new Timer();
锁定进程时还涉及安排一个计时器事件,该事件会重新启动根方法本身:
if (isWaiting &&
lockDialog == null)
{
lockDialog = new Dialog(this, android.R.style.Theme_Panel);
lockDialog.setCancelable(false);
lockDialog.show();
timerUnlock.schedule(new TimerTask() {
@Override
public void run() {
// Close it automatically if running.
m_setisWaiting(false);
}
}, 10000);
}
解锁时确保取消任何计时器,这可以防止先前的锁定计时器解锁未完成且尚未超时的更新锁:
else if (!isWaiting)
{
if (lockDialog != null) {
lockDialog.dismiss();
lockDialog = null;
}
timerUnlock.cancel();
}
最后,将在接口级别上运行的代码应该在主线程上 运行(否则会导致连续崩溃 - 可能是因为计时器正在完成并从单独的线程触发:
runOnUiThread(new Runnable() {
@Override
public void run() {
// Remove any timeouts still running.
if(progressBar != null) {
if(isWaiting)
progressBar.setVisibility(View.VISIBLE);
else
progressBar.setVisibility(View.GONE);
}
}
});
完整方法如下:
public void m_setisWaiting (boolean isWaiting) {
final Timer timerUnlock = new Timer();
if (isWaiting &&
lockDialog == null)
{
lockDialog = new Dialog(this, android.R.style.Theme_Panel);
lockDialog.setCancelable(false);
lockDialog.show();
timerUnlock.schedule(new TimerTask() {
@Override
public void run() {
// Close it automatically if running.
m_setisWaiting(false);
}
}, 10000);
}
else if (!isWaiting)
{
if (lockDialog != null) {
lockDialog.dismiss();
lockDialog = null;
}
timerUnlock.cancel();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// Remove any timeouts still running.
if(progressBar != null) {
if(isWaiting)
progressBar.setVisibility(View.VISIBLE);
else
progressBar.setVisibility(View.GONE);
}
}
});
}