简体   繁体   中英

Check if Spell Checker is on?

I am using Spell Checker in my Android App. If is is off in the Language and Input settings then the App crashes. It needs to be on for the App to work properly.

Is there a way where before using it I can place a check to verify that is the spell checker is on or turn it on directly from code?

You can simply check the instance of SpellCheckerSession for null and accordingly decide whether Spellchecker is turned ON or not:

Code Snippet:

final TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);

scs = tsm.newSpellCheckerSession(null, null, this, true);

if (scs != null) {
    scs.getSuggestions(new TextInfo(text.getText().toString()), 3);
} else {
    // Show the message to user
    Toast.makeText(this, "Please turn on the spell checker from setting", Toast.LENGTH_LONG).show();
    // You can even open the settings page for user to turn it ON
    ComponentName componentToLaunch = new ComponentName("com.android.settings",
                "com.android.settings.Settings$SpellCheckersSettingsActivity");
    Intent intent = new Intent();
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setComponent(componentToLaunch);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        this.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        // Error
    }
}

Hope it helps ツ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM