Android - How to get a Session from my Spell Checker Service?
<p>I am trying to implement a spell checker service <a href="https://developer.android.com/guide/topics/text/spell-checker-framework.html" rel="nofollow noreferrer">as described here</a> called <code>SampleSpellCheckerService</code> but it seems the tutorial is incomplete and the source code for it does not seem to be available.</p>

<p>I am struggling with how to get a session from my spell checker service in the <code>setSuggestionsFor()</code> method of my activity, as highlighted here:</p>

<pre><code>public class SpellCheckerSettingsActivity extends AppCompatActivity implements SpellCheckerSession.SpellCheckerSessionListener {

    private static final String LOG_TAG = SpellCheckerSettingsActivity.class.getSimpleName();

    private TextView textView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spell_checker_settings);

        final EditText editText = (EditText)findViewById(R.id.editText);

        textView = (TextView)findViewById(R.id.textView);

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fetchSuggestionsFor(editText.getText().toString());
            }
        });

        startService(new Intent(this, SampleSpellCheckerService.class));

    }

    private void fetchSuggestionsFor(String input){

        Log.d(LOG_TAG, "fetchSuggestionsFor(\"" + input + "\")");

        /***************************************************
         * 
         * This line is invalid. What do I replace it with?
         * 
         ***************************************************/
        SpellCheckerSession session = SampleSpellCheckerService.getSession();

        TextInfo[] textInfos = new TextInfo[]{ new TextInfo(input) };
        int suggestionsLimit = 5;
        session.getSentenceSuggestions(textInfos, suggestionsLimit);

    }

    @Override
    public void onGetSuggestions(SuggestionsInfo[] results) {

        Log.d(LOG_TAG, "onGetSuggestions(" + results + ")");

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText("Suggestions obtained (TODO - get from results[])");
            }
        });

    }

    @Override
    public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {

        Log.d(LOG_TAG, "onGetSentenceSuggestions(" + results + ")");

        if (results != null) {
            final StringBuffer sb = new StringBuffer("");
            for (SentenceSuggestionsInfo result : results) {
                int n = result.getSuggestionsCount();
                for (int i = 0; i &lt; n; i++) {
                    int m = result.getSuggestionsInfoAt(i).getSuggestionsCount();

                    for (int k = 0; k &lt; m; k++) {
                        sb.append(result.getSuggestionsInfoAt(i).getSuggestionAt(k))
                                .append("\n");
                    }
                    sb.append("\n");
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText(sb.toString());
                }
            });
        }

    }

    @Override
    public void onDestroy() {
        stopService(new Intent(this, SampleSpellCheckerService.class));
        super.onDestroy();
    }
}
</code></pre>

<p><strong>So what is the correct way to get a session from <code>SampleSpellCheckerService</code>?</strong></p>

<p>For completeness, here is my spell checker service class:</p>

<pre><code>public class SampleSpellCheckerService extends SpellCheckerService {

    public static final String LOG_TAG = SampleSpellCheckerService.class.getSimpleName();

    public SampleSpellCheckerService() {
        Log.d(LOG_TAG, "SampleSpellCheckerService");
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(LOG_TAG, "SampleSpellCheckerService.onCreate");
    }

    @Override
    public Session createSession() {

        Log.d(LOG_TAG, "createSession");

        return new AndroidSpellCheckerSession();
    }

    private static class AndroidSpellCheckerSession extends SpellCheckerService.Session {

        @Override
        public void onCreate() {

            Log.d(LOG_TAG, "AndroidSpellCheckerSession.onCreate");

        }



        @Override
        public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit) {

            Log.d(LOG_TAG, "onGetSentenceSuggestionsMultiple");

            SentenceSuggestionsInfo[] suggestionsInfos = null;
            //suggestionsInfo = new SuggestionsInfo();
            //... // look up suggestions for TextInfo
            return suggestionsInfos;
        }

        @Override
        public SuggestionsInfo onGetSuggestions(TextInfo textInfo, int suggestionsLimit) {

            Log.d(LOG_TAG, "onGetSuggestions");

            SuggestionsInfo suggestionsInfo = null;
            //suggestionsInfo = new SuggestionsInfo();
            //... // look up suggestions for TextInfo
            return suggestionsInfo;
        }

        @Override
        public void onCancel() {
            Log.d(LOG_TAG, "onCancel");
        }


    }
}
</code></pre>

<p>Here is my manifest:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"&gt;

    &lt;permission android:name="android.permission.BIND_TEXT_SERVICE" /&gt;

    &lt;application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"&gt;

        &lt;service
            android:name="com.example.SampleSpellCheckerService"
            android:label="@string/app_name"
            android:enabled="true"
            android:permission="android.permission.BIND_TEXT_SERVICE"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.service.textservice.SpellCheckerService" /&gt;
            &lt;/intent-filter&gt;

            &lt;meta-data
                android:name="android.view.textservice.scs"
                android:resource="@xml/spellchecker" /&gt;
        &lt;/service&gt;

        &lt;activity android:name="com.example.SpellCheckerSettingsActivity"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;
</code></pre>

<p>And here is my spellchecker.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;spell-checker
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/spellchecker_name"
    android:settingsActivity="com.example.SpellCheckerSettingsActivity"&gt;
    &lt;subtype
        android:label="@string/subtype_generic"
        android:subtypeLocale="en" /&gt;
    /&gt;
    &lt;subtype
        android:label="@string/subtype_generic"
        android:subtypeLocale="en_GB" /&gt;
    /&gt;
&lt;/spell-checker&gt;
</code></pre>

<p>NB - I am testing with a Samsung device.</p>