标签:Toast ... SHORT LONG DELAY LENGTH
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
结果,不言而喻。
标签:Toast,...,SHORT,LONG,DELAY,LENGTH 来源: https://www.cnblogs.com/kborid/p/14078744.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。