@@ -41,6 +41,7 @@ class ScreenMirrorService : LifecycleService() {
4141    private  var  mBitmap:  Bitmap ?  =  null 
4242    private  lateinit  var  orientationEventListener:  OrientationEventListener 
4343    private  var  isPortrait =  true 
44+     private  var  notificationId:  Int  =  0 
4445
4546    private  var  mMediaProjection:  MediaProjection ?  =  null 
4647    private  var  mImageReaderPortrait:  ImageReader ?  =  null 
@@ -74,14 +75,6 @@ class ScreenMirrorService : LifecycleService() {
7475                    }
7576                }
7677            }
77-         val  notification = 
78-             NotificationHelper .createServiceNotification(
79-                 this ,
80-                 " ${BuildConfig .APPLICATION_ID } .action.stop_screen_mirror"  ,
81-                 getString(R .string.screen_mirror_service_is_running),
82-             )
83-         val  id =  NotificationHelper .generateId()
84-         ServiceCompat .startForeground(this , id, notification, ServiceInfo .FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION )
8578    }
8679
8780    @SuppressLint(" WrongConstant"  )
@@ -96,6 +89,22 @@ class ScreenMirrorService : LifecycleService() {
9689            mResultData =  intent.parcelable(" data"  )
9790        }
9891
92+         //  Acquire MediaProjection before starting FGS with mediaProjection type to satisfy Android 14/15
93+         if  (mResultCode !=  - 1  &&  mResultData !=  null  &&  mMediaProjection ==  null ) {
94+             mMediaProjection =  mediaProjectionManager.getMediaProjection(mResultCode, mResultData!! )
95+         }
96+ 
97+         if  (notificationId ==  0 ) {
98+             notificationId =  NotificationHelper .generateId()
99+         }
100+         val  notification = 
101+             NotificationHelper .createServiceNotification(
102+                 this ,
103+                 " ${BuildConfig .APPLICATION_ID } .action.stop_screen_mirror"  ,
104+                 getString(R .string.screen_mirror_service_is_running),
105+             )
106+         ServiceCompat .startForeground(this , notificationId, notification, ServiceInfo .FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION )
107+ 
99108        mImageReaderHandlerThread =  HandlerThread (" ImageReader"  )
100109        mImageReaderHandlerThread?.start()
101110        handler =  Handler (mImageReaderHandlerThread!! .looper)
@@ -172,7 +181,9 @@ class ScreenMirrorService : LifecycleService() {
172181    }
173182
174183    private  fun  doMirror () {
175-         mMediaProjection =  mediaProjectionManager.getMediaProjection(mResultCode, mResultData!! )
184+         if  (mMediaProjection ==  null ) {
185+             mMediaProjection =  mediaProjectionManager.getMediaProjection(mResultCode, mResultData!! )
186+         }
176187        val  width = 
177188            if  (isPortrait) {
178189                widthPortrait
0 commit comments