android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@c745883 - permission denied

喜夏-厌秋 提交于 2019-12-04 01:01:38

I saw this error some time ago, when I updated a native&react app to SDK 26. The problem is the react-native function that creates the red dialog for development. That functions uses TYPE_SYSTEM_ALERT as their type, so you can't use an SDK level bigger than 25 in your hybrid app with that version of react-native, unless you patch that function so it no longer uses TYPE_SYSTEM_ALERT.

this is the react-native code in 0.48:

private void showNewError(
      final String message,
      final StackFrame[] stack,
      final int errorCookie,
      final ErrorType errorType) {
    UiThreadUtil.runOnUiThread(
        new Runnable() {
          @Override
          public void run() {
            if (mRedBoxDialog == null) {
              mRedBoxDialog = new RedBoxDialog(mApplicationContext, DevSupportManagerImpl.this, mRedBoxHandler);
              mRedBoxDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            }
            if (mRedBoxDialog.isShowing()) {
              // Sometimes errors cause multiple errors to be thrown in JS in quick succession. Only
              // show the first and most actionable one.
              return;
            }
            mRedBoxDialog.setExceptionDetails(message, stack);
            updateLastErrorInfo(message, stack, errorCookie, errorType);
            // Only report native errors here. JS errors are reported
            // inside {@link #updateJSError} after source mapping.
            if (mRedBoxHandler != null && errorType == ErrorType.NATIVE) {
              mRedBoxHandler.handleRedbox(message, stack, RedBoxHandler.ErrorType.NATIVE);
              mRedBoxDialog.resetReporting(true);
            } else {
              mRedBoxDialog.resetReporting(false);
            }
            mRedBoxDialog.show();
          }
        });
  } 

You would need to change the mRedBoxDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); call to other type. But, this does not give you any guarantees you'll be able to run your app with SDK 26, when I tried to compile react with SDK 26, other parts of the project "reacted" exploding, so this is unlikely to work at short term (you may need to start fixing other problems). So your options are:

-Downgrade the app to level 25;

-Upgrade to react 0.52, where that function no longer exist, and try again (react libraries may not work after this)

-Patch the function in react-native 0.48 branch and try the patched version. Some other issues related to the SDK may arose (react at this point is still being compiled with SDK 22)

Happy coding!

Elad
TYPE_SYSTEM_ALERT

This constant was deprecated in API level 26. for non-system apps. Use TYPE_APPLICATION_OVERLAY instead.

Verify SYSTEM_ALERT_WINDOW Permission Or try to provide Overlay Permission Manually.

As @Fco P. said you can try to change

mRedBoxDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

**## If you are using Lenovo device. Please try with another one. This particular permission makes problem sometime in Lenovo device.****

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!