Problems with custom Toast above Android6.0

Same as the previous article, the problem occurred after the device was replaced with Android9 version

android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@45f48c40 is not valid; is your activity running?

In the customization, the type is changed to

params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
later appeared again

android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@47dd171 -- permission denied for window type 2038

This is because there is no permission to set the display on the upper layer of other applications.

    public void checkPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(MinimalistModeActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, 10);
            }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 10) {
            if (Build.VERSION.SDK_INT >= 23) {
                if (!Settings.canDrawOverlays(this)) {
                    // SYSTEM_ALERT_WINDOW permission not granted...
                    Toast.makeText(MinimalistModeActivity.this, "not granted", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

plus, ok

Guess you like

Origin blog.csdn.net/lanrenxiaowen/article/details/111030822