簡體   English   中英

如何在 Android 中幾秒鍾后隱藏消息

[英]How to hide a message after few seconds in Android

我向用戶顯示消息通知並在幾秒鍾后隱藏該通知,我正在使用 postDelayed() 來實現此功能,但問題是當我在隱藏第一個消息之前用新消息更新通知時,我需要重置/重啟定時器。 我找不到任何方法來實現這一點,因為 removeCallbacks() 只刪除掛起的任務。

每次我有新消息要在按鈕單擊時顯示時,我showNotification()MyClass調用showNotification() 現在,如果我在 4 秒內(假設為x秒)(在隱藏舊通知消息之前)再次單擊該按鈕。 無法取消舊的 postDelayed 或將計時器重新啟動到 4 秒,它會在4-x秒后隱藏我的新通知。

我的類

...
// On Button Click - everytime a different message

MyNotification obj = new MyNotification();
obj.showNotification(msg);
...

我的通知.java

public class MyNotification
{
    ...
    ...
    private static TextView textview1;
    private static MyNotification notification;

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();

        notification = (MyNotification) this.findViewById(R.id.notification_bar);
        textview1 = (TextView) this.findViewById(R.id.textview_notification);
    }

    Runnable hideNotification = new Runnable() {
        @Override
        public void run() {
            textview1.setVisibility(GONE);
            notification.setVisibility(GONE);
        }
    };

    public void showNotification(String msg)
    {

        textview1.setText(msg);
        textview1.setVisibility(VISIBLE);
        notification.setVisibility(VISIBLE);

        // Hide Toast notification after few secs
        notification.postDelayed(hideNotification, 4000);
    }

    ...
}

只需使用處理程序並根據需要添加或刪除回調。 我為此寫了一個類。 我會與你分享。

   /**
     * Created with IntelliJ IDEA.
     * User: App Studio 35
     * Date: 5/17/13
     * Time: 1:53 PM
     * To change this template use File | Settings | File Templates.
     */

    public class TimeOutWatcher {

    /*//////////////////////////////////////////////////////////
    // MEMBERS
    *///////////////////////////////////////////////////////////
    private ITimeOutWatcher mTimeOutHandler = null;
    private Handler mHandler = null;
    private long mTimeOutMilliseconds;


    /*//////////////////////////////////////////////////////////
    // Runnable
    *///////////////////////////////////////////////////////////
    private Runnable mTimeoutRunnable = new Runnable() {
        @Override
        public void run() {
            //If stopTimeOutWatch has not been called then trigger TimeOutOccurred
            if(mTimeOutHandler != null){
                mTimeOutHandler.TimeOutOccurred();
            }

            //Remove callback to avoid duplicate triggers
            mHandler.removeCallbacks(mTimeoutRunnable);
        }
    };


   /*//////////////////////////////////////////////////////////
   // METHODS
   *///////////////////////////////////////////////////////////
    /**
     * Set TimeOutListener to trigger when timeout occurs
     *
     * @param listener of type ITimeOutWatcher
     */
    public void setTimeOutListener(ITimeOutWatcher listener){
        mTimeOutHandler = listener;
    }
    /**
     * Called when needing a timeout to occur based on the passed number of
     * Milliseconds. Performs a postDelay to trigger calling of TimeOutOccurred.
     * Must call stopTimeOutWatch after your waiting process is complete and
     * before this delay trigger happens if you want to avoid
     * getting a TimeOutOccurred
     *
     * @param timeOutMilliseconds
     */
    public void startTimeOutWatch(long timeOutMilliseconds){
        mTimeOutMilliseconds = timeOutMilliseconds;
        mHandler = new Handler(Looper.getMainLooper());
        mHandler.postDelayed(mTimeoutRunnable, timeOutMilliseconds);
    }
    public void resetTimeOutWatch(long timeOutMilliseconds){
        stopTimeOutWatch();
        startTimeOutWatch(timeOutMilliseconds);
    }
    public void resetTimeOutWatch(){
        stopTimeOutWatch();
        startTimeOutWatch(mTimeOutMilliseconds);
    }
    /**
     * Used to cancel TimeOut from happening
     * Call when process that you are waiting on is complete
     * to avoid TimeOutOccurred
     */
    public void stopTimeOutWatch(){
        if(mTimeoutRunnable != null){
            if(mHandler != null){
                mHandler.removeCallbacks(mTimeoutRunnable);
            }
        }
    }


    public interface ITimeOutWatcher {

        /**
         * Called when TimeOutWatcher exceeds requested TimeOut Period
         */
        void TimeOutOccurred();

    }
}

用法很簡單,只需創建一個 TimeOutWatcher 實例並在所需時間開始超時即可。 如果您需要取消它然后調用stop 如果你想重置然后調用reset。 簡單的。 希望有幫助。

一個簡單的解決方案是,如果在通知已顯示時單擊按鈕,則防止重復通知。

這可以通過在啟動 runnable 之前檢查視圖的可見性來輕松完成:

public void showNotification(String msg)
    {

        if (textview1.getVisibility() != View.VISIBLE) {
            textview1.setText(msg);
            textview1.setVisibility(VISIBLE);
            notification.setVisibility(VISIBLE);

            // Hide Toast notification after few secs
            notification.postDelayed(hideNotification, 4000);
        }
    }

當然你可以取消 runnable 並發布一個新的:使用View.removeCallbacks(Runnable)

所以我認為您的代碼會更改為:

public void showNotification(String msg)
{

    textview1.setText(msg);
    textview1.setVisibility(VISIBLE);
    notification.setVisibility(VISIBLE);

    // Remove the existing hide instruction
    notification.removeCallbacks(hideNotification);
    // Hide Toast notification after few secs
    notification.postDelayed(hideNotification, 4000);
}

暫無
暫無

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

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