Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法添加窗口-此窗口类型2003或2006的windowmanager权限被拒绝_Android_Android Service_Android Camera_Android Permissions - Fatal编程技术网

Android 无法添加窗口-此窗口类型2003或2006的windowmanager权限被拒绝

Android 无法添加窗口-此窗口类型2003或2006的windowmanager权限被拒绝,android,android-service,android-camera,android-permissions,Android,Android Service,Android Camera,Android Permissions,我正在开发一个带有后台服务的录像机应用程序。我发现以下错误: java.lang.RuntimeException: Unable to start service com.example.justbackgroundcamera.BackgroundVideoRecorder@82e8d33 with Intent { cmp=com.example.justbackgroundcamera/.BackgroundVideoRecorder (has extras) }: android.vi

我正在开发一个带有后台服务的录像机应用程序。我发现以下错误:

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);
windowManager=(windowManager)this.getSystemService(Context.WINDOW\u服务);
surfaceView=新的surfaceView(本);
ViewGroup.LayoutParams LayoutParams=新建WindowManager.LayoutParams(1,1,
Build.VERSION.SDK\u INT
舱单:

<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
调用的情况下在后台启动
服务

例如

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);

我已经解决了这个问题。在使用安卓M及以上版本时,我们需要一些权限。您可以使用“绘制其他应用”权限解决此问题

添加到清单

<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);
    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);
        }
    }