1+ page.title=Using the Spell Checker Framework
2+ parent.title=Articles
3+ parent.link=../browser.html?tag=article
4+ @jd:body
5+ <div id="qv-wrapper">
6+ <div id="qv">
7+ <h2>In This Document</h2>
8+ <ol>
9+ <li>
10+ <a href="#SpellCheckLifeCycle">Spell Check Lifecycle</a>
11+ </li>
12+ <li>
13+ <a href="#SpellCheckImplementation">Implementing a Spell Checker Service</a>
14+ </li>
15+ <li>
16+ <a href="#SpellCheckClient">Implementing a Spell Checker Client</a>
17+ </li>
18+ </ol>
19+ <h2>See also</h2>
20+ <ol>
21+ <li>
22+ <a href="{@docRoot}resources/samples/SpellChecker/SampleSpellCheckerService/index.html">
23+ Spell Checker Service</a> sample app
24+ </li>
25+ <li>
26+ <a href="{@docRoot}resources/samples/SpellChecker/HelloSpellChecker/index.html">
27+ Spell Checker Client</a> sample app
28+ </li>
29+ </ol>
30+ </div>
31+ </div>
32+
33+ <p>
34+ The Android platform offers a spell checker framework that lets you implement
35+ and access spell checking in your application. The framework is one of the
36+ Text Service APIs offered by the Android platform.
37+ </p>
38+ <p>
39+ To use the framework in your app, you create a special type of Android service that
40+ generates a spell checker <strong>session</strong> object. Based on text you provide,
41+ the session object returns spelling suggestions generated by the spell checker.
42+ </p>
43+ <h2 id="SpellCheckLifeCycle">Spell Checker Lifecycle</h2>
44+ <p>
45+ The following diagram shows the lifecycle of the spell checker service:
46+ </p>
47+ <img src="{@docRoot}resources/articles/images/spellcheck_lifecycle.png" alt="" height="596"
48+ id="figure1" />
49+ <p class="img-caption">
50+ <strong>Figure 1.</strong> The spell checker service lifecycle.
51+ </p>
52+ <p>
53+ To initiate spell checking, your app starts its implementation of the spell checker
54+ service. Clients in your app, such as activities or individual UI elements, request a
55+ spell checker session from the service, then use the session to get suggestions for text.
56+ As a client terminates its operation, it closes its spell checker session. If necessary, your
57+ app can shut down the spell checker service at any time.
58+ </p>
59+ <h2 id="SpellCheckImplementation">Implementing a Spell Checker Service</h2>
60+ <p>
61+ To use the spell checker framework in your app, add a spell checker service component including
62+ the session object definition. You can also add to your app an optional activity that
63+ controls settings. You must also add an XML metadata file that describes
64+ the spell checker service, and add the appropriate elements to your manifest file.
65+ </p>
66+ <h3 id="SpellCheckCode">Spell checker classes</h3>
67+ <p>
68+ Define the service and session object with the following classes:
69+ </p>
70+ <dl>
71+ <dt>
72+ A subclass of {@link android.service.textservice.SpellCheckerService}
73+ </dt>
74+ <dd>
75+ The {@link android.service.textservice.SpellCheckerService} implements both the
76+ {@link android.app.Service} class and the spell checker framework interface. Within your
77+ subclass, you must implement the following method:
78+ <dl>
79+ <dt>{@link android.service.textservice.SpellCheckerService#createSession()}</dt>
80+ <dd>
81+ A factory method that returns a
82+ {@link android.service.textservice.SpellCheckerService.Session} object to a
83+ client that wants to do spell checking.
84+ </dd>
85+ </dl>
86+ <p>
87+ See the
88+ <a href="{@docRoot}resources/samples/SpellChecker/SampleSpellCheckerService/index.html">
89+ Spell Checker Service</a> sample app to learn more about implementing this class.
90+ </p>
91+ </dd>
92+ <dt>
93+ An implementation of {@link android.service.textservice.SpellCheckerService.Session}
94+ </dt>
95+ <dd>
96+ An object that the spell checker service provides to clients, to let them pass text to
97+ the spell checker and receive suggestions. Within this class, you must implement the
98+ following methods:
99+ <dl>
100+ <dt>
101+ {@link android.service.textservice.SpellCheckerService.Session#onCreate()}
102+ </dt>
103+ <dd>
104+ Called by the system in response to
105+ {@link android.service.textservice.SpellCheckerService#createSession()}. In this
106+ method, you can initialize the
107+ {@link android.service.textservice.SpellCheckerService.Session} object based on
108+ the current locale and so forth.
109+ </dd>
110+ <dt>
111+ {@link android.service.textservice.SpellCheckerService.Session#onGetSuggestions(TextInfo, int)
112+ onGetSuggestions()}
113+ </dt>
114+ <dd>
115+ Does the actual spell checking. This method returns an object containing
116+ suggestions for the text passed to it.
117+ </dd>
118+ </dl>
119+ <p>
120+ Optionally, you can implement
121+ {@link android.service.textservice.SpellCheckerService.Session#onCancel()}, which
122+ handles requests to cancel spell checking, or
123+ {@link android.service.textservice.SpellCheckerService.Session#onGetSuggestionsMultiple(TextInfo[], int, boolean)
124+ onGetSuggestionsMultiple()}, which handles batches of suggestion requests, or both.
125+ </p>
126+ <p>
127+ See the
128+ <a href="{@docRoot}resources/samples/SpellChecker/HelloSpellChecker/index.html">
129+ Spell Checker Client</a> sample app to learn more about implementing this class.
130+ </p>
131+ </dd>
132+ </dl>
133+ <p class="note">
134+ <strong>Note:</strong> You must implement all aspects of spell checking as asynchronous and
135+ thread-safe. A spell checker may be called simultaneously by different threads running on
136+ different cores. The {@link android.service.textservice.SpellCheckerService} and
137+ {@link android.service.textservice.SpellCheckerService.Session} take care of this
138+ automatically.
139+ </p>
140+ <h3 id="SpellCheckXML">Spell checker manifest and metadata</h3>
141+ <p>
142+ In addition to code, you need to provide the appropriate manifest file and a metadata file for
143+ the spell checker.
144+ </p>
145+ <p>
146+ The manifest file defines the application, the service, and the activity for controlling
147+ settings, as shown in the following snippet:
148+ </p>
149+ <pre>
150+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
151+ package="com.example.android.samplespellcheckerservice" >
152+ <application
153+ android:label="@string/app_name" >
154+ <service
155+ android:label="@string/app_name"
156+ android:name=".SampleSpellCheckerService"
157+ android:permission="android.permission.BIND_TEXT_SERVICE" >
158+ <intent-filter >
159+ <action android:name="android.service.textservice.SpellCheckerService" />
160+ </intent-filter>
161+
162+ <meta-data
163+ android:name="android.view.textservice.scs"
164+ android:resource="@xml/spellchecker" />
165+ </service>
166+
167+ <activity
168+ android:label="@string/sample_settings"
169+ android:name="SpellCheckerSettingsActivity" >
170+ <intent-filter >
171+ <action android:name="android.intent.action.MAIN" />
172+ </intent-filter>
173+ </activity>
174+ </application>
175+ </manifest>
176+ </pre>
177+ <p>
178+ Notice that components that want to use the service must request the permission
179+ {@link android.Manifest.permission#BIND_TEXT_SERVICE} to ensure that only the system binds to
180+ the service. The service's definition also specifies the <code>spellchecker.xml</code> metadata
181+ file, which is described in the next section.
182+ </p>
183+ <p>
184+ The metadata file <code>spellchecker.xml</code> contains the following XML:
185+ </p>
186+ <pre>
187+ <spell-checker xmlns:android="http://schemas.android.com/apk/res/android"
188+ android:label="@string/spellchecker_name"
189+ android:settingsActivity="com.example.SpellCheckerSettingsActivity">
190+ <subtype
191+ android:label="@string/subtype_generic"
192+ android:subtypeLocale="en”
193+ />
194+ <subtype
195+ android:label="@string/subtype_generic"
196+ android:subtypeLocale="fr”
197+ />
198+ </spell-checker>
199+ </pre>
200+ <p>
201+ The metadata specifies the activity that the spell checker uses for controlling settings. It
202+ also defines subtypes for the spell checker; in this case, the subtypes define locales that
203+ the spell checker can handle.
204+ </p>
205+
206+
207+ <!-- Accessing the Spell Checker Service from a Client -->
208+ <h2 id="SpellCheckClient">Accessing the Spell Checker Service from a Client</h2>
209+ <p>
210+ Applications that use {@link android.widget.TextView} views automatically benefit from spell
211+ checking, because {@link android.widget.TextView} automatically uses a spell checker. The
212+ following screenshots show this:
213+ </p>
214+ <img src="{@docRoot}resources/articles/images/textview_spellcheck_screenshot_1.png" alt=""
215+ height="45" id="figure2a" />
216+ <br>
217+ <img src="{@docRoot}resources/articles/images/textview_spellcheck_screenshot_2.png" alt=""
218+ height="121" id="figure2b" />
219+ <p class="img-caption">
220+ <strong>Figure 2.</strong> Spell checking in TextView.
221+ </p>
222+ <p>
223+ However, you may want to interact directly with a spell checker service in other cases as well.
224+ The following diagram shows the flow of control for interacting with a spell checker service:
225+ </p>
226+ <img src="{@docRoot}resources/articles/images/spellcheck_client_flow.png" alt=""
227+ height="394" id="figure3" />
228+ <p class="img-caption">
229+ <strong>Figure 3.</strong> Interacting with a spell checker service.
230+ </p>
231+ <p>
232+ The <a href="{@docRoot}resources/samples/SpellChecker/HelloSpellChecker/index.html">
233+ Spell Checker Client</a> sample app shows how to interact with a spell checker service. The
234+ LatinIME input method editor in the Android Open Source Project also contains an example of
235+ spell checking.
236+ </p>
0 commit comments