[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Spell checker framework\n\nThe Android platform offers a spell checker framework that lets you\nimplement and access spell checking in your app. The framework is one of the\nText Service APIs.\n\nTo use the framework in your app, you create an Android service that\ngenerates a spell checker *session* object. Based on text you provide,\nthe session object returns spelling suggestions generated by the spell\nchecker.\n\nSpell checker lifecycle\n-----------------------\n\nThe following diagram shows the lifecycle of the spell checker service:\n**Figure 1.** The spell checker service lifecycle.\n\nTo initiate spell checking, your app starts its implementation of the spell\nchecker service. Clients in your app, such as activities or individual UI\nelements, request a spell checker session from the service, then use the session\nto get suggestions for text. As a client terminates its operation, it closes\nits spell checker session. If necessary, your app can shut down the spell\nchecker service at any time.\n\nImplement a spell checker service\n---------------------------------\n\nTo use the spell checker framework in your app, add a spell checker service\ncomponent that includes the session object definition. You can also add an\noptional activity to your app that controls settings. Add an XML metadata file\nthat describes the spell checker service, and add the appropriate elements to\nyour manifest file.\n\n### Spell checker classes\n\nDefine the service and session object with the following classes:\n\n| **Note:** Implement all aspects of spell checking asynchronously and in a thread-safe manner. A spell checker might be called simultaneously by different threads running on different cores. The `SpellCheckerService` and `SpellCheckerService.Session` take care of this automatically.\n\n### Spell checker manifest and metadata\n\nIn addition to code, provide the appropriate manifest file and a metadata\nfile for the spell checker.\n\nThe manifest file defines the app, the service, and the activity for\ncontrolling settings, as shown in the following example: \n\n```xml\n\u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n package=\"com.example.android.samplespellcheckerservice\" \u003e\n \u003capplication\n android:label=\"@string/app_name\" \u003e\n \u003cservice\n android:label=\"@string/app_name\"\n android:name=\".SampleSpellCheckerService\"\n android:permission=\"android.permission.BIND_TEXT_SERVICE\" \u003e\n \u003cintent-filter \u003e\n \u003caction android:name=\"android.service.textservice.SpellCheckerService\" /\u003e\n \u003c/intent-filter\u003e\n\n \u003cmeta-data\n android:name=\"android.view.textservice.scs\"\n android:resource=\"@xml/spellchecker\" /\u003e\n \u003c/service\u003e\n\n \u003cactivity\n android:label=\"@string/sample_settings\"\n android:name=\"SpellCheckerSettingsActivity\" \u003e\n \u003cintent-filter \u003e\n \u003caction android:name=\"android.intent.action.MAIN\" /\u003e\n \u003c/intent-filter\u003e\n \u003c/activity\u003e\n \u003c/application\u003e\n\u003c/manifest\u003e\n```\n\nComponents that want to use the service must request the permission\n[BIND_TEXT_SERVICE](/reference/android/Manifest.permission#BIND_TEXT_SERVICE)\nto ensure that only the system binds to the service. The service's definition\nalso specifies the `spellchecker.xml` metadata file, which is\ndescribed in the next section.\n\nThe metadata file `spellchecker.xml` contains the following\nXML: \n\n```xml\n\u003cspell-checker xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:label=\"@string/spellchecker_name\"\n android:settingsActivity=\"com.example.SpellCheckerSettingsActivity\"\u003e\n \u003csubtype\n android:label=\"@string/subtype_generic\"\n android:subtypeLocale=\"en”\n /\u003e\n \u003csubtype\n android:label=\"@string/subtype_generic\"\n android:subtypeLocale=\"fr”\n /\u003e\n\u003c/spell-checker\u003e\n```\n\nThe metadata specifies the activity that the spell checker uses to control\nsettings. It also defines subtypes for the spell checker. In this case, the\nsubtypes define locales that the spell checker can handle.\n\nAccess the spell checker service from a client\n----------------------------------------------\n\napps that use\n[TextView](/reference/android/widget/TextView) and\n[EditText](/reference/android/widget/EditText)\nviews automatically benefit from spell checking, because `TextView`\nautomatically uses a spell checker:\n**Figure 2.** Spell checking in an `EditText`.\n\nHowever, you might want to interact directly with a spell checker service in\nother cases. The following diagram shows the flow of control for interacting\nwith a spell checker service:\n**Figure 3.** Interaction with a spell checker service.\n\nThe\n[LatinIME\ninput method editor in the Android Open Source Project](https://android.googlesource.com/platform/packages/inputmethods/LatinIME/) contains an example of\nspell checking."]]