android 10.0录制屏幕报错. Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE
-
-
- 报错原因
- 报错解决 (这里默认你已经写好了正常的录制屏幕流程)
- Android技术生活交流
-
- 更多其他页面-自定义View-实用功能合集:点击查看
报错原因
Android10.0以上的录制屏幕需要获取到FOREGROUND_SERVICE权限
Android10.0以上实例化mediaProjection需要在service里进行
Android10.0以上录制屏幕需要添加notification,提醒用户该app正在录制屏幕
Caused by: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
报错解决 (这里默认你已经写好了正常的录制屏幕流程)
- 在
AndroidManifest.xml
内的service
添加foregroundServiceType
android:foregroundServiceType="mediaProjection"
- 在申请录屏幕权限后的返回数据
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;}
- 构造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群