Android12当你需要一个时间超长的Toast

本文介绍了Android系统中对Toast功能的增强,新增了5分钟持续显示的选项,同时详细描述了不同长度Toast的处理逻辑,以及与NotificationManagerService和PhoneWindowManager服务的集成,确保了toast的显示顺序和超时管理。
摘要由CSDN通过智能技术生成

根据客户需求,我这里增加了一个5分钟的Toast,用于提示升级

diff --git a/core/api/current.txt b/core/api/current.txt
index 41c9565..8e35e1a 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -56626,6 +56626,7 @@ package android.widget {
     method public void setText(CharSequence);
     method @Deprecated public void setView(android.view.View);
     method public void show();
+    field public static final int LENGTH_5MIN = 2; // 0x2
     field public static final int LENGTH_LONG = 1; // 0x1
     field public static final int LENGTH_SHORT = 0; // 0x0
   }
diff --git a/core/java/android/widget/Toast.java b/core/java/android/widget/Toast.java
index 862829b..5deb9a3 100644
--- a/core/java/android/widget/Toast.java
+++ b/core/java/android/widget/Toast.java
@@ -89,7 +89,8 @@ public class Toast {
     /** @hide */
     @IntDef(prefix = { "LENGTH_" }, value = {
             LENGTH_SHORT,
-            LENGTH_LONG
+            LENGTH_LONG,
+        LENGTH_5MIN
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface Duration {}
@@ -108,6 +109,8 @@ public class Toast {
      */
     public static final int LENGTH_LONG = 1;
 
+    public static final int LENGTH_5MIN = 2;
+
     /**
      * Text toasts will be rendered by SystemUI instead of in-app, so apps can't circumvent
      * background custom toast restrictions.
diff --git a/core/java/android/widget/ToastPresenter.java b/core/java/android/widget/ToastPresenter.java
index eccff06..c227d82 100644
--- a/core/java/android/widget/ToastPresenter.java
+++ b/core/java/android/widget/ToastPresenter.java
@@ -52,6 +52,7 @@ public class ToastPresenter {
     // exclusively used to guarantee window timeouts
     private static final long SHORT_DURATION_TIMEOUT = 4000;
     private static final long LONG_DURATION_TIMEOUT = 7000;
+    private static final long FIVEMIN_DURATION_TIMEOUT = 300000;
 
     @VisibleForTesting
     public static final int TEXT_TOAST_LAYOUT = R.layout.transient_notification;
@@ -153,8 +154,19 @@ public class ToastPresenter {
         params.horizontalMargin = horizontalMargin;
         params.verticalMargin = verticalMargin;
         params.packageName = mContext.getPackageName();
-        params.hideTimeoutMilliseconds =
-                (duration == Toast.LENGTH_LONG) ? LONG_DURATION_TIMEOUT : SHORT_DURATION_TIMEOUT;
+        //params.hideTimeoutMilliseconds =
+        //        (duration == Toast.LENGTH_LONG) ? LONG_DURATION_TIMEOUT : SHORT_DURATION_TIMEOUT;
+    switch(duration) {
+        case Toast.LENGTH_SHORT:
+        params.hideTimeoutMilliseconds = SHORT_DURATION_TIMEOUT;
+        break;
+        case Toast.LENGTH_LONG:
+        params.hideTimeoutMilliseconds = LONG_DURATION_TIMEOUT;
+        break;
+        case Toast.LENGTH_5MIN:
+        params.hideTimeoutMilliseconds = FIVEMIN_DURATION_TIMEOUT;
+        break;
+    }
         params.token = windowToken;
 
         if (removeWindowAnimations && params.windowAnimations == R.style.Animation_Toast) {
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index 51f2ba4..6d548f7 100755
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -366,6 +366,7 @@ public class NotificationManagerService extends SystemService {
 
     static final int LONG_DELAY = TOAST_WINDOW_TIMEOUT - TOAST_WINDOW_ANIM_BUFFER; // 3.5 seconds
     static final int SHORT_DELAY = 2000; // 2 seconds
+    static final int FIVEMIN_DELAY = 300000;
 
     // 1 second past the ANR timeout.
     static final int FINISH_TOKEN_TIMEOUT = 11 * 1000;
@@ -7823,7 +7824,19 @@ public class NotificationManagerService extends SystemService {
     {
         mHandler.removeCallbacksAndMessages(r);
         Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r);
-        int delay = r.getDuration() == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;
+        //int delay = r.getDuration() == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;
+    int delay = 0;
+        switch(r.getDuration()) {
+           case Toast.LENGTH_SHORT:
+               delay = SHORT_DELAY;
+               break;
+           case Toast.LENGTH_LONG:
+           delay = LONG_DELAY;
+               break;
+           case Toast.LENGTH_5MIN:
+           delay = FIVEMIN_DELAY;
+               break;
+        }
         // Accessibility users may need longer timeout duration. This api compares original delay
         // with user's preference and return longer one. It returns original delay if there's no
         // preference.
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index bc499ab..007f8dd 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -344,7 +344,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {
       * Amount of time (in milliseconds) a toast window can be shown before it's automatically
       * removed by window manager.
       */
-    public static final int TOAST_WINDOW_TIMEOUT = 3500 + TOAST_WINDOW_ANIM_BUFFER;
+    //public static final int TOAST_WINDOW_TIMEOUT = 3500 + TOAST_WINDOW_ANIM_BUFFER;
+    public static final int TOAST_WINDOW_TIMEOUT = 300000 + TOAST_WINDOW_ANIM_BUFFER;
 
     /**
      * Lock protecting internal state.  Must not call out into window

实际调用的方法和原始Toast相同

mToast = Toast.makeText(mContext, comment, Toast.LENGTH_5MIN);
mToast.show();

这里需要注意的是,在这个5分钟toast显示时间内,其他的toast只会排队,不会覆盖显示,直到5分钟结束。所以如果需要更新toast,记得要先把5分钟的这个取消掉mToast.cancel();

  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值