Android 14 だと mediaProjection.createVirtualDisplay で例外が出る。
java.lang.SecurityException: Don't re-use the resultData to retrieve the same projection instance, and don't use a token that has timed out. Don't take multiple captures by invoking MediaProjection#createVirtualDisplay multiple times on the same instance.
https://stackoverflow.com/questions/76804553/reusing-media-projection-intent-on-android-14
明らかに制限が増えてて操作感が悪くなりそう
さらに
java.lang.SecurityException:
Starting FGS with type mediaProjection
callerApp=ProcessRecord{7fd65d0 17027:jp.juggler.screenshotbutton/u0a225}
targetSDK=34
requires permissions:
all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION]
any of the permissions allOf=false [android.permission.CAPTURE_VIDEO_OUTPUT, android:project_media]
とか。
- foregroundServiceType が mediaProjection のサービスを開始するには CAPTURE_VIDEO_OUTPUT 権限が必要。
- CAPTURE_VIDEO_OUTPUT 権限の取得は通常の実行時権限要求ではなく、mediaProjectionManager.createScreenCaptureIntent() を使った確認UIを割と頻繁に呼び出す必要がある。
サービスが何かの理由で失われたから少し後に再起動しよう…と思っても権限だったりScreenCaptureIntentの返すトークンだったりが不足して不可能な感じ。