any body knows why this error occur, Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION on getMediaProjection() ev
Add android:foregroundServiceType="mediaProjection"
to the <service>
element in the manifest for your service.
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.....
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)
}