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

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

报错原因

  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"


2,在申请录屏幕权限后的返回数据onActivityResult内对sdk进行判断,并实现startForegroundService。

这样我们就可以在service内进行初始化mediaProjection了

    @Override
    protected 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应该为 null
                screenRecordService.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了。

 @Override
    public 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;
    }

3.构造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(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        startForeground(110, notification);

    }


————————————————

原文链接:https://blog.csdn.net/ruiruiddd/article/details/117952037

猜你喜欢

转载自blog.csdn.net/weixin_42602900/article/details/128340037
今日推荐