Android require a foreground service of type ServiceInfo.FOREGROUND_SERVICE

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

  • 更多其他页面-自定义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
    @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;
    }
  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(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        startForeground(110, notification);

    }

Android技术生活交流

微信 ----- qq群



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值