簡體   English   中英

在設定的時間后刪除對話框

[英]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;

        }

    }

}

正如上面代碼中的注釋所指出的,這確實有效,並且可以正確執行我需要的操作。 我只是想知道我是否要附加處理程序,或者是否需要進行任何其他清理以確保活動代碼中不會留下垃圾。 不太熟悉處理程序。

更新:我想確保如果關閉對話框(此方法輸入的參數為false),則該處理程序不會觸發。 因此,如果進行錯誤檢查,則如果處理程序的發布延遲,則該處理程序應與對話框一起關閉。 我必須確保上一個請求的發布延遲超時不會拒絕以后的請求。

已解決,答案由我自己發布-感謝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);
            }
        }
    });

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM