[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Foreground service timeouts\n\nIf an app targets Android 15 or higher, the system places\nrestrictions on how long certain foreground services are allowed to run while\nyour app is in the background. Currently, this restriction only applies to\n[`dataSync`](/develop/background-work/services/fgs/service-types#data-sync) and\n[`mediaProcessing` foreground service type](/develop/background-work/services/fgs/service-types#media-processing) foreground\nservices. There are more restrictive limits on the [`shortService` foreground\nservice type](/develop/background-work/services/fgs/service-types#short-service) which are discussed in that service type's\ndocumentation.\n\nTimeout behavior\n----------------\n\nThe system permits `dataSync` and `mediaProcessing` foreground services to run\nfor a total of 6 hours in a 24-hour period, after which the system calls the\nrunning service's [`Service.onTimeout(int, int)`](/reference/android/app/Service#onTimeout(int,%20int)) method\n(introduced in Android 15). (The `mediaProcessing` foreground\nservice type was added in Android 15.) The six-hour time limit is\ntracked separately for `dataSync` and for `mediaProcessing` services. For\nexample, if a `dataSync` service just ran for one hour, the app would only have\nfive hours available for `dataSync` foreground services, but it would\nhave a full six hours available for `mediaProcessing` services.\n| **Note:** `shortService` foreground services have a more restrictive time limit. For more information, see the [short service documentation](/develop/background-work/services/fgs/service-types#short-service).\n\nWhen a foreground service reaches the six-hour limit, the service has a few\nseconds to call [`Service.stopSelf()`](/reference/android/app/Service#stopSelf()). When the system calls\n`Service.onTimeout()`, the service is no longer considered a foreground service.\nIf the service does not call `Service.stopSelf()`, the system throws an internal\nexception. The exception is logged in [Logcat](/tools/logcat) with the following\nmessage: \n\n Fatal Exception: android.app.RemoteServiceException: \"A foreground service of\n type [service type] did not stop within its timeout: [component name]\"\n\n| **Note:** The 6-hour time limit is shared by all of an app's foreground services of the specified type. For example, if an app runs a `dataSync` service for four hours, then starts a different `dataSync` service, that second service will only be allowed to run for two hours. However, if the user brings the app to the foreground, the timer resets and the app has 6 hours available.\n\nTo avoid problems with this behavior change, you can do one or more of the\nfollowing:\n\n1. Have your service implement the new `Service.onTimeout(int, int)` method. When your app receives the callback, make sure to call `stopSelf()` within a few seconds. (If you don't stop the app right away, the system generates a failure.)\n2. Make sure your app's `dataSync` and `mediaProcessing` services don't run for more than a total of 6 hours in any 24-hour period (unless the user interacts with the app, resetting the timer).\n3. Only start `dataSync` or `mediaProcessing` foreground services as a result of direct user interaction; since your app is in the foreground when the service starts, your service has the full six hours after the app goes to the background.\n4. Instead of using these foreground services, use an use an [alternative\n API](/develop/background-work/background-tasks#alternative-apis), like WorkManager. In particular, instead of using a `dataSync` foreground service, consider using an [alternative API](/develop/background-work/background-tasks/data-transfer-options).\n\nIf your app's `dataSync` foreground services have run for 6 hours in the last\n24, you cannot start another `dataSync` foreground service *unless* the user\nhas brought your app to the foreground (which resets the timer). If you try to\nstart another `dataSync` foreground service, the system throws\n[`ForegroundServiceStartNotAllowedException`](/reference/android/app/ForegroundServiceStartNotAllowedException)\nwith an error message like \"Time limit already exhausted for foreground service\ntype dataSync\".\n\nTesting\n-------\n\nTo test your app's behavior, you can enable data sync timeouts even if your app\nis not targeting Android 15 (as long as the app is running on an Android 15\ndevice). To enable timeouts, run the following [`adb`](/tools/adb) command: \n\n adb shell am compat enable FGS_INTRODUCE_TIME_LIMITS \u003cvar label=\"package-name\" translate=\"no\"\u003eyour-package-name\u003c/var\u003e\n\nYou can also adjust the timeout period, to make it easier to test how your\napp behaves when the limit is reached. To set a new timeout period for\n`dataSync` foreground services, run the following `adb` command: \n\n adb shell device_config put activity_manager data_sync_fgs_timeout_duration \u003cvar label=\"timeout-duration\" translate=\"no\"\u003eduration-in-milliseconds\u003c/var\u003e\n\nTo set a new timeout period for `mediaProcessing` foreground services, run this\ncommand: \n\n adb shell device_config put activity_manager media_processing_fgs_timeout_duration \u003cvar label=\"timeout-duration\" translate=\"no\"\u003eduration-in-milliseconds\u003c/var\u003e"]]