@@ -449,25 +449,53 @@ private void realShow() {
449
449
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N_MR1 ) {
450
450
mWM = (WindowManager ) context .getSystemService (Context .WINDOW_SERVICE );
451
451
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
+ }
452
459
} else {
453
460
Context topActivityOrApp = UtilsBridge .getTopActivityOrApp ();
454
461
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 ();
456
465
return ;
457
466
}
458
467
Activity topActivity = (Activity ) topActivityOrApp ;
459
468
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 ();
461
472
return ;
462
473
}
463
474
mWM = topActivity .getWindowManager ();
464
475
mParams .type = WindowManager .LayoutParams .LAST_APPLICATION_WINDOW ;
465
476
UtilsBridge .addActivityLifecycleCallbacks (topActivity , getActivityLifecycleCallbacks ());
466
477
}
467
478
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 () {
468
496
mParams .height = WindowManager .LayoutParams .WRAP_CONTENT ;
469
497
mParams .width = WindowManager .LayoutParams .WRAP_CONTENT ;
470
- mParams .format = PixelFormat .TRANSLUCENT ;
498
+ mParams .format = PixelFormat .TRANSPARENT ;
471
499
mParams .windowAnimations = android .R .style .Animation_Toast ;
472
500
mParams .setTitle ("ToastWithoutNotification" );
473
501
mParams .flags = WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON
@@ -487,19 +515,6 @@ private void realShow() {
487
515
mParams .y = mToast .getYOffset ();
488
516
mParams .horizontalMargin = mToast .getHorizontalMargin ();
489
517
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 );
503
518
}
504
519
505
520
private Utils .ActivityLifecycleCallbacks getActivityLifecycleCallbacks () {
0 commit comments