Hey,
I’m finding a small number of devices running One UI 2.0 incorrectly throw the exception:
java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
This occurs when attempting to get a media projection object, after having prompted the user and received a code through onActivityResult.
mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
Intent permissionIntent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(permissionIntent, Constants.REQUEST_SCREEN_CAPTURE_CODE);…
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {if (Constants.REQUEST_SCREEN_CAPTURE_CODE == requestCode) { if (resultCode == RESULT_OK) { try { mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, intent); } catch (SecurityException e) { // java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION }
I DO already declare the service as a mediaProjection service type:
// AndroidManifest.xml
<service android:name=".services.MyService" android:enabled="true" android:exported="false" android:foregroundServiceType="mediaProjection" />
And then within MyService’s onStartCommand
I do the following:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { startForeground(Constants.FOREGROUND_SERVICE, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION); } else { startForeground(Constants.FOREGROUND_SERVICE, notification); }
This is NOT impacting all phones running One UI 2.0. It only appears to be a small number of them. Is there somewhere else I should be reporting this? It does appear to be occurring across multiple phone carriers (TMobile, Verizon, etc), so I doubt it’s due to carrier modifications.
Also, this is NOT occurring on any other android 10 devices.