Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE TYPE_MEDIA_PROJECTION in Android Pie and Q

后端 未结 3 1385
长情又很酷
长情又很酷 2021-01-05 18:09

any body knows why this error occur, Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION on getMediaProjection() ev

3条回答
  •  春和景丽
    2021-01-05 18:38

    Wrapping getMediaProjection(mResultCode, mResultData) inside a Handler().postDelayed() solved the problem for me.

    override fun onServiceConnected(className: ComponentName, service: IBinder) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            val binder = service as MediaProjectionService.LocalBinder
            mediaProjectionService = binder.getService()
    
            // delay needed because getMediaProjection() throws an error if it's called too soon
            Handler().postDelayed({
                mediaProjection = mediaProjectionManager.getMediaProjection(activityResultCode, activityData)
                startStreaming()
                serviceBound = true
            }, 1000)
        }
    

提交回复
热议问题