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

后端 未结 3 1299
长情又很酷
长情又很酷 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:28

    Add android:foregroundServiceType="mediaProjection" to the <service> element in the manifest for your service.

    0 讨论(0)
  • 2021-01-05 18:30

    Please make sure you have granted the required FOREGROUND_SERVICE permissions. If your targetSdkVersion = 28 using foreground services must request the FOREGROUND_SERVICE permission. So add FOREGROUND_SERVICE permission to your manifest file without which it throws SecurityException.

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    

    You can read more about this migration notes: https://developer.android.com/about/versions/pie/android-9.0-migration#tya

    Hope this will help you.....

    0 讨论(0)
  • 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)
        }
    
    0 讨论(0)
提交回复
热议问题