Skip to end of metadata
Go to start of metadata

When building screen sharing application targeted to Android 10 (API level 29) and newer, it is necessary to launch a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION before requesting permissions to capture screeen.

private void startScreenCapture() {
    mMediaProjectionManager = (MediaProjectionManager) getSystemService(
            Context.MEDIA_PROJECTION_SERVICE);
    Intent permissionIntent = mMediaProjectionManager.createScreenCaptureIntent();
    startService(new Intent(StreamingMinActivity.this, TestService.class));
    startActivityForResult(permissionIntent, REQUEST_CODE_CAPTURE_PERM);
}
private void showNotification() {
    Intent notificationIntent = new Intent(this, StreamingMinActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    int iconId = R.mipmap.ic_launcher;
    int uniqueCode = new Random().nextInt(Integer.MAX_VALUE);
    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(iconId)
            .setContentText("Started stream")
            .setContentIntent(pendingIntent).build();
    startForeground(uniqueCode, notification, FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);
}
  • No labels