You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 14下原生暗滤镜应用崩溃求助:权限适配疑问

Android 14 暗滤镜应用崩溃解决方案

报错核心原因

你的应用在Android 14上崩溃是因为启动mediaProjection类型的前台服务时权限不满足,报错信息明确要求:

Starting FGS with type mediaProjection ... requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION] any of the permissions allOf=false [android.permission.CAPTURE_VIDEO_OUTPUT, android:project_media]

拆解权限要求:

  • 必须拥有android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION(mediaProjection类型前台服务的基础权限)
  • 二选一权限:CAPTURE_VIDEO_OUTPUT(系统应用专属,第三方无法申请)或PROJECT_MEDIA(第三方应用可用的屏幕投影权限)

具体修复步骤

  1. 在Manifest中声明权限
    在AndroidManifest.xml中添加以下权限配置:

    <!-- 声明mediaProjection类型前台服务权限 -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
    <!-- 第三方可用的屏幕投影权限 -->
    <uses-permission android:name="android.permission.PROJECT_MEDIA" />
    
  2. 动态申请PROJECT_MEDIA权限
    Android 14中PROJECT_MEDIA属于危险权限,需在代码中动态申请,示例代码:

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.PROJECT_MEDIA)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityResultContracts.RequestPermission requestPermission = new ActivityResultContracts.RequestPermission();
        registerForActivityResult(requestPermission, isGranted -> {
            if (isGranted) {
                // 权限授予后启动服务
                startService(new Intent(this, ShakeOverlayService.class));
            } else {
                Toast.makeText(this, "需要开启PROJECT_MEDIA权限以运行暗滤镜", Toast.LENGTH_SHORT).show();
            }
        }).launch(Manifest.permission.PROJECT_MEDIA);
    } else {
        // 权限已存在,直接启动服务
        startService(new Intent(this, ShakeOverlayService.class));
    }
    
  3. 指定前台服务类型
    在ShakeOverlayService的onStartCommand方法中,启动前台服务时必须明确指定类型:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 自定义前台通知(需符合Android通知规范)
        Notification notification = buildForegroundNotification();
        // 指定服务类型为mediaProjection
        startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);
        // 其他服务逻辑
        return START_STICKY;
    }
    

关于CAPTURE_VIDEO_OUTPUT权限

该权限为系统应用专属,第三方应用无法通过正常渠道获取,直接忽略即可,无需在Manifest中声明。

内容的提问来源于stack exchange,提问作者The Academic Weapon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 23:46:05