【问题标题】:Unable to add window - windowmanager permission denied for this window type 2003 or 2006无法添加窗口 - 此窗口类型 2003 或 2006 的窗口管理器权限被拒绝
【发布时间】:2019-10-30 20:09:15
【问题描述】:

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

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 android-service android-camera android-permissions


    【解决方案1】:

    信息太少。因此,请确保您的应用程序支持 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);
    

    【讨论】:

    • 谢谢,但我正在使用通知。我知道我的权限有问题。所以我解决了这个问题。
    【解决方案2】:

    我已经解决了这个问题。在使用 Android 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);
    

    添加到您的代码(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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 2018-07-09
      • 2019-02-03
      • 2018-02-22
      • 1970-01-01
      相关资源
      最近更新 更多