android 10.0录制屏幕报错. Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE

  • 报错原因
  • 报错解决 (这里默认你已经写好了正常的录制屏幕流程)
  • Android技术生活交流
  • 更多其他页面-自定义View-实用功能合集:点击查看

报错原因

  1. Android10.0以上的录制屏幕需要获取到FOREGROUND_SERVICE权限

  2. Android10.0以上实例化mediaProjection需要在service里进行

  3. Android10.0以上录制屏幕需要添加notification,提醒用户该app正在录制屏幕

Caused by: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION


报错解决 (这里默认你已经写好了正常的录制屏幕流程)

  1. AndroidManifest.xml内的service添加foregroundServiceType
android:foregroundServiceType="mediaProjection"
  1. 在申请录屏幕权限后的返回数据onActivityResult内对sdk进行判断,并实现startForegroundService,这样我们就可以在service内进行初始化mediaProjection
    @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {screenRecordService.setMediaProject(MediaProject); //将MediaProject传进service里,这个medieaProject应该为 nullscreenRecordService.setMediaProjectionManager(MediaProjectionManager); //将mediaProjectionManager传进service里,这个manager你应该已经在`ServiceConnection`时实例化好了()Intent service = new Intent(this, ScreenRecordService.class);service.putExtra("code", resultCode);service.putExtra("data", data);startForegroundService(service);}}

调过startForegroundService会执行service的 onStartCommand 方法,之后我们就可以在service里进行实例化 MediaProject

 @Overridepublic int onStartCommand(Intent intent, int flags, int startId) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {createNotificationChannel(); //创建通知栏,你正在录屏Bundle bundle = intent.getExtras();MediaProject = mediaProjectionManager.getMediaProjection( bundle.getInt("code",-1), Objects.requireNonNull(intent.getParcelableExtra("data")));if(null != mOnStartCommandListener){mOnStartCommandListener.finished(mediaProjection!=null);}}return START_STICKY;}
  1. 构造Notification
    private void createNotificationChannel() {Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器Intent nfIntent = new Intent(this, TutorW2Activity.class); //点击后跳转的界面,可以设置跳转数据builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher)) // 设置下拉列表中的图标(大图标)//.setContentTitle("SMI InstantView") // 设置下拉列表里的标题.setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标.setContentText("is running......") // 设置上下文内容.setWhen(System.currentTimeMillis()); // 设置该通知发生的时间/*以下是对Android 8.0的适配*///普通notification适配if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {builder.setChannelId("notification_id");}//前台服务notification适配if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);NotificationChannel channel = new NotificationChannel("notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);notificationManager.createNotificationChannel(channel);}Notification notification = builder.build(); // 获取构建好的Notificationnotification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音startForeground(110, notification);}

Android技术生活交流

微信 ----- qq群


Android require a foreground service of type ServiceInfo.FOREGROUND_SERVICE相关推荐

  1. Android 录屏录制功能:require a foreground service of type ServiceInfo.FOREGROUND_SERVICE

    android 10.0录制屏幕报错. Media projections require a foreground service of type ServiceInfo.FOREGROUND_SE ...

  2. Android 10 Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TY

    Android 10 在使用MediaProjectionManager录音的时候报错: java.lang.SecurityException: Media projections require ...

  3. Android Foreground Service (前台服务)-by:nixs

    一.如何保活后台服务 在Android Services (后台服务) 里面,我们了解了Android四大组件之一的Service,知道如何使用后台服务进行来完成一些特定的任务.但是后台服务在系统内存 ...

  4. 邊實驗邊分析 - Android Foreground Service的使用

    邊實驗邊分析 - Android Foreground Service的使用 一.簡述 二.Android LowMemoryKiller 介紹 三.startService方法的調用行爲測試 給Se ...

  5. android Foreground Service 前台服务/notification全局通知

    android Foreground Service 前台服务/notification全局通知 前言 要素简介 前台服务(Foreground Service) 全局通知(notification) ...

  6. Android 中的context, service,active和intent使用详解

    在一个Android应用中,主要是由四种组件组成的,这四种组件分别是Context,Activity,Intent,Service. Content被译为上下文,是应用程序中心,应用程序所有功能可以通 ...

  7. 深入分析Android 9.0源代码——Service启动流程(startService方式)

    引言 点击此处查看<深入分析Android 9.0源代码>系列的组织结构和相关说明. 1 应用进程发起启动请求 本章的调用流程如下图所示: (Context)ContextWrapperC ...

  8. 浅谈Android四大组件之Service

    一:Service简介 Android开发中,当需要创建在后台运行的程序的时候,就要使用到Service. 1:Service(服务)是一个没有用户界面的在后台运行执行耗时操作的应用组件.其他应用组件 ...

  9. Android之四大组件(Service的开启与关闭)

    个人开发的微信小程序,目前功能是书籍推荐,后续会完善一些新功能,希望大家多多支持! 前言 服务(Service)是Android系统中的四大组件之一.服务主要用于两个目的:后台运行和跨进程访问.通过启 ...

最新文章

  1. JS实例学习笔记——w3cschool+菜鸟教程
  2. wireshark tcp data中文_TCP的可靠传输
  3. web---jsp连接数据库
  4. tomcat 环境配置
  5. 手写一个promise用法_手写一个Promise
  6. 数组求和forEach方法
  7. Blog运用感想 (ZT)
  8. linux安装yarn
  9. Java 编写的 坦克大战小游戏
  10. 高德api地图的调用
  11. 当win10电脑,本地网络出现了一个意外的情况,不能完成所有你在设置中所要求的更改?
  12. 小程序报错:[渲染层网络层错误] Failed to load local image resource /static/logo.png......
  13. img图片在webpack中使用
  14. xampp mysql远程连接_XAMPP mysql远程连接
  15. python预测股票价格_使用机器学习预测股票价格的愚蠢简便方法
  16. 浙江大学电子信息计算机,浙江大学城市学院计算机与计算科学学院 党政办 计算学院新增12名浙江大学电子信息专业硕士研究生导师...
  17. 一文带你吃透微服务守门神SpringCloud-GateWay
  18. 电脑文件管理,教你批量给全部文件夹名称随机命名
  19. 苹果备忘录显示无法连接服务器失败,备忘录帮助
  20. ElasticSearch之监控工具-cerebro

热门文章

  1. 暗通道去雾 python实现
  2. ignite缓存使用
  3. Java实现 蓝桥杯VIP 算法提高 质数的后代
  4. 谈谈数据库的隔离方式
  5. linux中mongodb下载及安装
  6. 将md文件转换为HTML页面
  7. 金蝶K3加密许可记录清除策略浅析
  8. 了不起的 Webpack Scope Hoisting 学习指南
  9. mapreduceyarn的工作机制----吸星大法
  10. VC++ 使用StackWalker类打印当前运行堆栈信息