【Android】Toast对应的LENGTH_SHORT和LENGTH_LONG分别是多长时间

Toast对应的LENGTH_SHORT和LENGTH_LONG分别是多长时间?

我们经常使用Toast,通过`LENGTH_SHORT`或者`LENGTH_LONG`,但没特别注意具体显示多长时间。今天突然看了一下`Toast.java`源码(`android-28`),发现代码定义如下:

/**
     * Show the view or text notification for a short period of time.  This time
     * could be user-definable.  This is the default.
     * @see #setDuration
     */
    public static final int LENGTH_SHORT = 0;

    /**
     * Show the view or text notification for a long period of time.  This time
     * could be user-definable.
     * @see #setDuration
     */
    public static final int LENGTH_LONG = 1;

大写懵逼,`0`和`1`是什么鬼?

继续往下找……

`makeText`方法没有什么实质性调用,主要是解析布局,设置参数,

直接看`Toast`的`show`方法,

/**
     * Show the view for the specified duration.
     */
    public void show() {
        if (mNextView == null) {
            throw new RuntimeException("setView must have been called");
        }

        INotificationManager service = getService();
        String pkg = mContext.getOpPackageName();
        TN tn = mTN;
        tn.mNextView = mNextView;

        try {
            service.enqueueToast(pkg, tn, mDuration);
        } catch (RemoteException e) {
            // Empty
        }
    }

继续跟,这里通过`getService()`拿到了`NotivicationManagerService`的代理`INotificationManager`,

static private INotificationManager getService() {
        if (sService != null) {
            return sService;
        }
        sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification"));
        return sService;
    }

然后调用了`service.enqueueToast(pkg, tn, mDuration);`,直接看`NotificationManagerService`的`enqueueToast`方法,

@Override
        public void enqueueToast(String pkg, ITransientNotification callback, int duration)
        {
           //...略...
            synchronized (mToastQueue) {
                    //...略...
                    // If it's at index 0, it's the current toast.  It doesn't matter if it's
                    // new or just been updated.  Call back and tell it to show itself.
                    // If the callback fails, this will remove it from the list, so don't
                    // assume that it's valid after this.
                    if (index == 0) {
                        showNextToastLocked();
                    }
               //...略...
            }
        }

重点关注这个方法`showNextToastLocked()`,辗转调用到`scheduleDurationReachedLocked`方法,开始了`toast`的显示,继续跟下去,真是柳暗花明又一村……

前边是构建`ToastRecord`,这里通过`Handler`进行显示了

@GuardedBy("mToastQueue")
    private void scheduleDurationReachedLocked(ToastRecord r)
    {
        mHandler.removeCallbacksAndMessages(r);
        Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r);
        long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;
        mHandler.sendMessageDelayed(m, delay);
    }

终于发现了最终的`duration`的真面目。

long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;

在`NotificationManagerService.java`中定义,

static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT;
    static final int SHORT_DELAY = 2000; // 2 seconds

而`LONG_DELAY`定义在`PhoneWindowManager`中

    /** Amount of time (in milliseconds) a toast window can be shown. */
    public static final int TOAST_WINDOW_TIMEOUT = 3500; // 3.5 seconds

结果,不言而喻。

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值