0

I made popups during an incoming voice call. For versions above Android 8.0 it gives the following error:

Unable to add window android.view.ViewRootImpl$W@e5b2272 -- permission denied for window type 2003

This link describes the solution, but I use customdiaolog. Link:Android: Unable to add window. Permission denied for this window type

 if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
        return;

        // Popup
    else {
        this.context = context;
        if(dialog == null){
            dialog = new CustomDialog(context);
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |

                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            dialog.show();
        }

 telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        listener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                String stateString = "N/A";
                switch (state) {
                    case TelephonyManager.CALL_STATE_IDLE:
                        stateString = "Idle";
                        dialog.dismiss();
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        stateString = "Off Hook";
                        dialog.dismiss();
                        break;
                    case TelephonyManager.CALL_STATE_RINGING:
                        stateString = "Ringing";
                        dialog.show();
                        break;
                }
            }
        };
Nikaido
  • 4,443
  • 5
  • 30
  • 47
Selena
  • 83
  • 7

1 Answers1

0

You need to have ACTION_MANAGE_OVERLAY_PERMISSION permission to open/display Alert when your app is in background or not open.

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

  <uses-permission
        android:name="android.permission.INTERNAL_SYSTEM_WINDOW"
        tools:ignore="ProtectedPermissions" />

set alert type of "TYPE_APPLICATION_OVERLAY".

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
            }else{
                dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            }

You must check this answer. If still you have doubt inform me

Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30
  • @Selena have you tried with ACTION_MANAGE_OVERLAY_PERMISSION ?? – Bhoomika Patel Sep 27 '19 at 09:38
  • I added the permission to my manifest file. But it didn't. How do I show the ACTION_MANAGE_OVERLAY_PERMISSION permission to the user from the settings page? – Selena Sep 27 '19 at 11:59
  • Its will not show in Setting page. you need to take permission programatically when user is in app. Check this: https://stackoverflow.com/questions/58112575/pop-up-wiindow-with-notification-even-apllication-is-not-running/58112860#58112860 – Bhoomika Patel Sep 27 '19 at 12:00
  • I could not look at the link you said. I cannot redirect to the Setting page. Can you help me please ? – Selena Sep 27 '19 at 12:22
  • https://stackoverflow.com/questions/32558153/how-to-make-alertdialog-view-in-input-method-service/52902433#52902433 Follow steps i mentioned here – Bhoomika Patel Sep 27 '19 at 12:30