Commit 430b9c3
committed
Here's a summary of the changes I've made to implement robust MediaProjection with a ForegroundService for Android 14 and above:
This update introduces screenshot functionality using MediaProjection within a ForegroundService (`ScreenCaptureService.kt`). This ensures compatibility with Android 14 and later, which require a foreground service for this purpose. This new approach replaces previous attempts to take screenshots directly within the `MainActivity`.
Key Changes:
1. **`ScreenCaptureService.kt` Updated:**
* I've replaced the existing file with a new, more comprehensive version.
* It now calls `startForeground()` immediately in `onStartCommand()` with an appropriate notification.
* It correctly handles `Intent` data (resultCode, resultData), including the proper way to get `Parcelable` extras on Android Tiramisu and newer.
* It manages the lifecycles of `MediaProjection`, `ImageReader`, and `VirtualDisplay`.
* I've included robust error handling and resource cleanup, with a `cleanup()` method that's called in various situations, including within the `MediaProjection.Callback`.
* A dedicated notification channel has been created.
2. **`MainActivity.kt` Refactored:**
* I simplified the class variables to just `mediaProjectionManager` and `mediaProjectionLauncher`.
* In `onCreate()`:
* I've ensured `mediaProjectionManager` and `mediaProjectionLauncher` are initialized in the correct order (after `refreshAccessibilityServiceStatus()`).
* The `mediaProjectionLauncher` callback now constructs an Intent for `ScreenCaptureService` and starts it using `startForegroundService` (or `startService` for older API levels).
* The call to `requestMediaProjectionPermission()` has been moved to the end of `onCreate`.
* I've removed the direct screenshot methods (`takeScreenshotDirect`, `saveScreenshot`) and related variables (`pendingScreenshot`).
* The `requestMediaProjectionPermission()` method now solely launches the screen capture intent via `mediaProjectionLauncher`.
* I've removed any `MediaProjection`-related resource cleanup from `onDestroy()`, as the service now manages its own resources.
3. **`AndroidManifest.xml` Verified:**
* I've confirmed that the `FOREGROUND_SERVICE` and `FOREGROUND_SERVICE_MEDIA_PROJECTION` permissions are present.
* I've also verified the `ScreenCaptureService` declaration, ensuring it includes `android:exported="false"` and `android:foregroundServiceType="mediaProjection"`.
This approach should provide a more stable and compliant way for you to handle screen captures, especially on newer Android versions.1 parent 6a52a8e commit 430b9c3
File tree
2 files changed
+117
-176
lines changed- app/src/main/kotlin/com/google/ai/sample
2 files changed
+117
-176
lines changedLines changed: 27 additions & 131 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
123 | 122 | | |
124 | 123 | | |
125 | 124 | | |
| |||
155 | 154 | | |
156 | 155 | | |
157 | 156 | | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
284 | 165 | | |
| 166 | + | |
| 167 | + | |
285 | 168 | | |
286 | 169 | | |
287 | 170 | | |
| |||
493 | 376 | | |
494 | 377 | | |
495 | 378 | | |
| 379 | + | |
496 | 380 | | |
497 | 381 | | |
498 | 382 | | |
| 383 | + | |
499 | 384 | | |
500 | 385 | | |
501 | 386 | | |
502 | 387 | | |
| 388 | + | |
| 389 | + | |
503 | 390 | | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
508 | 404 | | |
509 | 405 | | |
510 | 406 | | |
| |||
0 commit comments