[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Run AIDL services dynamically\n\nStarting in Android 11, native AIDL services running in\nthe system partition can be started and stopped dynamically as they are needed.\nDynamic services start when they are first requested and automatically stop when\nthey are no longer in use.\n| **Warning:** Due to Java garbage collection, Java-based services might take a long time to shut down. If this is problematic, you might want to avoid Java client code for dynamic services.\n\nServices that can run dynamically\n---------------------------------\n\nThis feature is available only for native services whose lifecycles can be\ncontrolled by `init` and `servicemanager`. Services within app packages are not\nsupported and should use [bound services](https://developer.android.com/guide/components/bound-services)\ninstead.\n\nDynamic shutdown works by shutting down the process in which the service runs.\nIf multiple services exist in the same process, all of them must be registered\nas dynamic to be compatible with this feature. That process will then shut down\nwhen all of the services are unused.\n\nConfigure a service's init .rc file\n-----------------------------------\n\nTo run a service dynamically, add the following options to the service's init\n`.rc` file after the leading `service \u003cname\u003e \u003ccmd\u003e` line. \n\n interface aidl serviceName\n disabled\n oneshot\n\nThese options do the following:\n\n- `interface aidl serviceName`: Allows `servicemanager` to find the service. If the service uses multiple interfaces, declare each interface on its own line. These names must be exactly what `servicemanager` expects and may differ from the process name.\n- `disabled`: Prevents the service from automatically starting at boot.\n- `oneshot`: Prevents the service from automatically restarting each time it is stopped.\n\nFor more information, see the [Android Init Language\nReadme](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/core/init/README.md)\nin AOSP.\n\nExamples:\n\n- [`apexd`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/apex/apexd/apexd.rc;drc=fa1746e827d09cf22b5dc9e60c48792da0c4e320;l=2)\n- [`gsid`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/gsid/gsid.rc;drc=67a2709553ab5d687da9af346b66e77bd7e4f1c1;l=2)\n\nRegister a service\n------------------\n\nEach service is created and registered with `servicemanager`. Registration often\noccurs in a file named `main.cpp`, but the implementation can vary. The\nregistration usually looks something like this: \n\n using android::defaultServiceManager;\n\n defaultServiceManager()-\u003eaddService(serviceName, service);\n\nRegistration is sometimes abstracted by `BinderService::publish` or\n`BinderService::instantiate`, which call the above code.\n\nTo register a service as dynamic, replace its registration code with the\nfollowing: \n\n #include \u003cbinder/LazyServiceRegistrar.h\u003e\n\n using android::binder::LazyServiceRegistrar;\n\n auto lazyRegistrar = LazyServiceRegistrar::getInstance();\n lazyRegistrar.registerService(service, serviceName);\n\n`servicemanager` communicates with `LazyServiceRegistrar` to shut down services\nbased on their reference counts.\n\nExamples:\n\n- [`apexservice`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/apex/apexd/apexservice.cpp;drc=b68f18b7ebc93617c44411b9bc906811501798c3;l=952)\n- [`gsi_service`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/gsid/gsi_service.cpp;drc=fc817e25fe44f3c62aa273268d9e2c2e9057699c;l=76)\n\nConfigure AIDL service clients\n------------------------------\n\n### Get the service\n\nTo retrieve a lazy service, the service must be started and then retrieved.\nCalling `getService` on the service manager will start the service, but usually,\nyou want to get the service as soon as it is available, and `waitForService`\nvariants should be used. See [backend-specific\ndocumentation](/docs/core/architecture/aidl/aidl-backends#service-management)\non how to use these.\n\n### Release the service\n\nDynamic shutdown is based on reference counting, so clients must not hold onto\nthe service when it is not in use.\n\nExamples:\n\n- [`ApexManager`](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/pm/ApexManager.java;l=434;drc=0037e9c816a6086bdf80019f59c130f0f0eb9fc0)\n- [`libgsid`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/gsid/libgsid.cpp;drc=fc817e25fe44f3c62aa273268d9e2c2e9057699c;l=30)\n\nTemporarily disable shutdown\n----------------------------\n\nIf you want a service to run independently until certain tasks are complete and\nthen switch to dynamic behavior, you can use\n`LazyServiceRegistrar::forcePersist` to toggle dynamic shutdown on and off. If\nthis is called from the server side, it should be called before\n`registerService`.\n\nExample: [`apexservice`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/apex/apexd/apexservice.cpp;drc=b68f18b7ebc93617c44411b9bc906811501798c3;l=951)"]]