首页 > 解决方案 > Android 10:android:foregroundServiceType="mediaProjection" 无法使用服务

问题描述

媒体投影需要 ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION 类型的前台服务

我正在尝试使用MediaProjection API捕获屏幕,但我的应用程序不断崩溃。我试过并给出了这个错误:

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

我的服务代码:

  class RecordService : Service() {
        private var mServiceHandler: ServiceHandler? = null
        private var mMediaProjection: MediaProjection? = null
        private var mVirtualDisplay: VirtualDisplay? = null
        private var mMediaRecorder: MediaRecorder? = null
        private var resultCode = 0
        private var data: Intent? = null
        private var mScreenStateReceiver: BroadcastReceiver? = null
    var isInitialized=false
    
        private inner class ServiceHandler(looper: Looper?) : Handler(looper) {
            override fun handleMessage(msg: Message?) {
                if (resultCode == RESULT_OK) {
                    startRecording(resultCode, data)
                } else {
                }
            }
        }
    
        override fun onCreate() {

            // when the main app is being closed
            val notificationIntent = Intent(this, RecordService::class.java)
            val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)
            val notification: Notification = Notification.Builder(this)
                .setContentTitle("DataRecorder")
                .setContentText("Your screen is being recorded and saved to your phone.")
                .setSmallIcon(R.drawable.ic_dialog_alert)
                .setContentIntent(pendingIntent)
                .setTicker("Tickertext")
                .build()
            isInitialized=true
            startForeground(ONGOING_NOTIFICATION_ID, notification)
  
            mScreenStateReceiver = MyBroadcastReceiver()
            val screenStateFilter = IntentFilter()
            screenStateFilter.addAction(Intent.ACTION_SCREEN_ON)
            screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF)
            screenStateFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED)
            registerReceiver(mScreenStateReceiver, screenStateFilter)
            val thread = HandlerThread(
                "ServiceStartArguments",
                THREAD_PRIORITY_BACKGROUND
            )
            thread.start()
            val mServiceLooper = thread.looper
            mServiceHandler = ServiceHandler(mServiceLooper)
        }
    
        override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
            Toast.makeText(this, "Starting recording service", Toast.LENGTH_SHORT).show()
            resultCode = intent.getIntExtra(EXTRA_RESULT_CODE, 0)
            data = intent.getParcelableExtra(EXTRA_DATA)
            check(!(resultCode == 0 || data == null)) { "Result code or data missing." }
            val msg = mServiceHandler!!.obtainMessage()
            msg.arg1 = startId
            mServiceHandler!!.sendMessage(msg)
            return START_REDELIVER_INTENT
        }
    
        private fun startRecording(resultCode: Int, data: Intent?) {
            val mProjectionManager =
                applicationContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
            mMediaRecorder = MediaRecorder()
            val metrics = DisplayMetrics()
            val wm =
                applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            wm.defaultDisplay.getRealMetrics(metrics)
            val mScreenDensity = metrics.densityDpi
            val displayWidth = metrics.widthPixels
            val displayHeight = metrics.heightPixels
            mMediaRecorder!!.setVideoSource(MediaRecorder.VideoSource.SURFACE)
            mMediaRecorder!!.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
            mMediaRecorder!!.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
            mMediaRecorder!!.setVideoEncodingBitRate(8 * 1000 * 1000)
            mMediaRecorder!!.setVideoFrameRate(15)
            mMediaRecorder!!.setVideoSize(displayWidth, displayHeight)
            val videoDir =
                Environment.getExternalStoragePublicDirectory(DIRECTORY_MOVIES)
                    .absolutePath
            val timestamp = System.currentTimeMillis()
            var orientation = "portrait"
            if (displayWidth > displayHeight) {
                orientation = "landscape"
            }
            val filePathAndName =
                videoDir + "/time_" + timestamp.toString() + "_mode_" + orientation + ".mp4"
            mMediaRecorder!!.setOutputFile(filePathAndName)
            try {
                mMediaRecorder!!.prepare()
            } catch (e: IllegalStateException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            }
            if(isInitialized){
                mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data!!)
                val surface: Surface = mMediaRecorder!!.surface
                mVirtualDisplay = mMediaProjection!!.createVirtualDisplay(
                    "MainActivity",
                    displayWidth,
                    displayHeight,
                    mScreenDensity,
                    VIRTUAL_DISPLAY_FLAG_PRESENTATION,
                    surface,
                    null,
                    null
                )
                mMediaRecorder!!.start()
                Log.v(TAG, "Started recording")
    
            }
           }
    
        companion object {
            private const val TAG = "RECORDERSERVICE"
            private const val EXTRA_RESULT_CODE = "resultcode"
            private const val EXTRA_DATA = "data"
            private const val ONGOING_NOTIFICATION_ID = 23
  
        }
    }

我也添加android:foregroundServiceType="mediaProjection"了清单,但它不起作用。我尝试了其他类似的任务,但问题尚未解决。谢谢

标签: androidscreen-capturescreen-recordingandroid-mediaprojection

解决方案


最后,我得到了关键:您必须首先在 onStartCommand 中创建 NotificationChannel,如下所示,否则您将收到“FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION 错误”。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    LogUtil.d(TAG, " onStartCommand intent = " + intent);
    createNotificationChannel();
    
    //do MediaProjection things that you want
    return START_NOT_STICKY;
}


private void createNotificationChannel() {
    Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
    Intent nfIntent = new Intent(this, MainActivity.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);
}

推荐阅读