无法添加 window - window管理员权限拒绝此 window 类型 2003 或 2006

Unable to add window - windowmanager permission denied for this window type 2003 or 2006

我正在开发带有后台服务的录像机应用程序。我收到这些错误:

java.lang.RuntimeException: Unable to start service com.example.justbackgroundcamera.BackgroundVideoRecorder@82e8d33 with Intent { cmp=com.example.justbackgroundcamera/.BackgroundVideoRecorder (has extras) }: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@959f86d -- permission denied for window type 2003

代码:

    windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
    surfaceView = new SurfaceView(this);
    ViewGroup.LayoutParams layoutParams = new WindowManager.LayoutParams(1, 1,
            Build.VERSION.SDK_INT < Build.VERSION_CODES.O ?
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT :
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
    // layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
    windowManager.addView(surfaceView, layoutParams);
    surfaceView.getHolder().addCallback(this);

清单:

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

我认为权限方面存在一些错误。我如何获得这些权限?

信息太少。因此,请确保您的应用程序支持 Android >= O 的限制。因为在这种情况下,您无法在没有通知和 startForeground 调用的情况下在后台启动 Services。

例如。

final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? getNotificationChannel(manager) : "";
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
final Notification notification = notificationBuilder.setOngoing(true)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setCategory(NotificationCompat.CATEGORY_SERVICE)
        .build();

startForeground(110, notification);

我已经解决了这个问题。使用 Android M 及更高版本时,我们需要一些权限。您可以使用 "draw over other apps" 权限解决此问题。

添加到清单

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

窗口管理器

    windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
    surfaceView = new SurfaceView(this);
    ViewGroup.LayoutParams layoutParams = new WindowManager.LayoutParams(1, 1,
            Build.VERSION.SDK_INT < Build.VERSION_CODES.O ?
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY :
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
    windowManager.addView(surfaceView, layoutParams);
    surfaceView.getHolder().addCallback(this);

添加到您的代码 (onCreate)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 0);
        }
    }