@@ -50,7 +50,7 @@ public class ToastStrategy implements IToastStrategy {
5050 private static final Handler HANDLER = new Handler (Looper .getMainLooper ());
5151
5252 /** 应用上下文 */
53- private Application mApplication ;
53+ private final Application mApplication ;
5454
5555 /** Toast 对象 */
5656 private WeakReference <IToast > mToastReference ;
@@ -61,11 +61,12 @@ public class ToastStrategy implements IToastStrategy {
6161 /** 上一个 Toast 显示的时间 */
6262 private volatile long mLastShowToastMillis ;
6363
64- public ToastStrategy () {
65- this (ToastStrategy .SHOW_STRATEGY_TYPE_IMMEDIATELY );
64+ public ToastStrategy (Application application ) {
65+ this (application , ToastStrategy .SHOW_STRATEGY_TYPE_IMMEDIATELY );
6666 }
6767
68- public ToastStrategy (int type ) {
68+ public ToastStrategy (Application application , int type ) {
69+ mApplication = application ;
6970 mShowStrategyType = type ;
7071 switch (mShowStrategyType ) {
7172 case SHOW_STRATEGY_TYPE_IMMEDIATELY :
@@ -76,45 +77,42 @@ public ToastStrategy(int type) {
7677 }
7778 }
7879
79- @ Override
80- public void registerStrategy (Application application ) {
81- mApplication = application ;
82- }
83-
8480 @ Override
8581 public int computeShowDuration (CharSequence text ) {
8682 return text .length () > 20 ? Toast .LENGTH_LONG : Toast .LENGTH_SHORT ;
8783 }
8884
8985 @ Override
9086 public IToast createToast (ToastParams params ) {
91- Activity toastActivity = getToastActivity ();
87+ final Application application = getApplication ();
88+ final Activity toastActivity = getToastActivity ();
89+
9290 IToast toast ;
9391 if ((params .priorityType == ToastParams .PRIORITY_TYPE_DEFAULT ||
9492 params .priorityType == ToastParams .PRIORITY_TYPE_GLOBAL ) &&
9593 Build .VERSION .SDK_INT >= Build .VERSION_CODES .M &&
96- Settings .canDrawOverlays (mApplication )) {
94+ Settings .canDrawOverlays (application )) {
9795 // 如果有悬浮窗权限,就开启全局的 Toast
98- toast = new GlobalToast (mApplication );
96+ toast = new GlobalToast (application );
9997 } else if (toastActivity != null &&
10098 (params .priorityType == ToastParams .PRIORITY_TYPE_DEFAULT ||
10199 params .priorityType == ToastParams .PRIORITY_TYPE_LOCAL )) {
102100 // 如果没有悬浮窗权限,就开启一个依附于 Activity 的 Toast
103101 toast = new ActivityToast (toastActivity );
104102 } else if (Build .VERSION .SDK_INT == Build .VERSION_CODES .N_MR1 ) {
105103 // 处理 Android 7.1 上 Toast 在主线程被阻塞后会导致报错的问题
106- toast = new SafeToast (mApplication );
104+ toast = new SafeToast (application );
107105 } else if (Build .VERSION .SDK_INT < Build .VERSION_CODES .Q &&
108- !areNotificationsEnabled (mApplication )) {
106+ !areNotificationsEnabled (application )) {
109107 // 处理 Toast 关闭通知栏权限之后无法弹出的问题
110108 // 通过查看和对比 NotificationManagerService 的源码
111109 // 发现这个问题已经在 Android 10 版本上面修复了
112110 // 但是 Toast 只能在前台显示,没有通知栏权限后台 Toast 仍然无法显示
113111 // 并且 Android 10 刚好禁止了 Hook 通知服务
114112 // 已经有通知栏权限,不需要 Hook 系统通知服务也能正常显示系统 Toast
115- toast = new NotificationToast (mApplication );
113+ toast = new NotificationToast (application );
116114 } else {
117- toast = new SystemToast (mApplication );
115+ toast = new SystemToast (application );
118116 }
119117 if (areSupportCustomToastStyle (toast ) || !onlyShowSystemToastStyle ()) {
120118 diyToastStyle (toast , params .style );
@@ -182,7 +180,7 @@ protected boolean areSupportCustomToastStyle(IToast toast) {
182180 * 定制 Toast 的样式
183181 */
184182 protected void diyToastStyle (IToast toast , IToastStyle <?> style ) {
185- toast .setView (style .createView (mApplication ));
183+ toast .setView (style .createView (toast . getContext () ));
186184 toast .setGravity (style .getGravity (), style .getXOffset (), style .getYOffset ());
187185 toast .setMargin (style .getHorizontalMargin (), style .getVerticalMargin ());
188186 }
@@ -349,6 +347,13 @@ protected boolean areNotificationsEnabled(Context context) {
349347 return true ;
350348 }
351349
350+ /**
351+ * 获取 Application 对象
352+ */
353+ protected Application getApplication () {
354+ return mApplication ;
355+ }
356+
352357 /**
353358 * 获取显示自定义 Toast 的 Activity 对象
354359 */
0 commit comments