@@ -449,25 +449,53 @@ private void realShow() {
449449 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N_MR1 ) {
450450 mWM = (WindowManager ) context .getSystemService (Context .WINDOW_SERVICE );
451451 mParams .type = WindowManager .LayoutParams .TYPE_TOAST ;
452+ } else if (UtilsBridge .isGrantedDrawOverlays ()) {
453+ mWM = (WindowManager ) context .getSystemService (Context .WINDOW_SERVICE );
454+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
455+ mParams .type = WindowManager .LayoutParams .TYPE_APPLICATION_OVERLAY ;
456+ } else {
457+ mParams .type = WindowManager .LayoutParams .TYPE_PHONE ;
458+ }
452459 } else {
453460 Context topActivityOrApp = UtilsBridge .getTopActivityOrApp ();
454461 if (!(topActivityOrApp instanceof Activity )) {
455- Log .e ("ToastUtils" , "Couldn't get top Activity." );
462+ Log .w ("ToastUtils" , "Couldn't get top Activity." );
463+ // try to use system toast
464+ new SystemToast (mToast ).show ();
456465 return ;
457466 }
458467 Activity topActivity = (Activity ) topActivityOrApp ;
459468 if (topActivity .isFinishing () || topActivity .isDestroyed ()) {
460- Log .e ("ToastUtils" , topActivity + " is useless" );
469+ Log .w ("ToastUtils" , topActivity + " is useless" );
470+ // try to use system toast
471+ new SystemToast (mToast ).show ();
461472 return ;
462473 }
463474 mWM = topActivity .getWindowManager ();
464475 mParams .type = WindowManager .LayoutParams .LAST_APPLICATION_WINDOW ;
465476 UtilsBridge .addActivityLifecycleCallbacks (topActivity , getActivityLifecycleCallbacks ());
466477 }
467478
479+ setToastParams ();
480+
481+ try {
482+ if (mWM != null ) {
483+ mWM .addView (mView , mParams );
484+ }
485+ } catch (Exception ignored ) {/**/ }
486+
487+ UtilsBridge .runOnUiThreadDelayed (new Runnable () {
488+ @ Override
489+ public void run () {
490+ cancel ();
491+ }
492+ }, mToast .getDuration () == Toast .LENGTH_SHORT ? 2000 : 3500 );
493+ }
494+
495+ private void setToastParams () {
468496 mParams .height = WindowManager .LayoutParams .WRAP_CONTENT ;
469497 mParams .width = WindowManager .LayoutParams .WRAP_CONTENT ;
470- mParams .format = PixelFormat .TRANSLUCENT ;
498+ mParams .format = PixelFormat .TRANSPARENT ;
471499 mParams .windowAnimations = android .R .style .Animation_Toast ;
472500 mParams .setTitle ("ToastWithoutNotification" );
473501 mParams .flags = WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON
@@ -487,19 +515,6 @@ private void realShow() {
487515 mParams .y = mToast .getYOffset ();
488516 mParams .horizontalMargin = mToast .getHorizontalMargin ();
489517 mParams .verticalMargin = mToast .getVerticalMargin ();
490-
491- try {
492- if (mWM != null ) {
493- mWM .addView (mView , mParams );
494- }
495- } catch (Exception ignored ) {/**/ }
496-
497- UtilsBridge .runOnUiThreadDelayed (new Runnable () {
498- @ Override
499- public void run () {
500- cancel ();
501- }
502- }, mToast .getDuration () == Toast .LENGTH_SHORT ? 2000 : 3500 );
503518 }
504519
505520 private Utils .ActivityLifecycleCallbacks getActivityLifecycleCallbacks () {
0 commit comments