Domanda

In my application I need to make the user choose an input method. Once it's selected I should perform some task. How is detected that the user actually chooses an InputMethod?

This is the code used to show the InputMethod list.

InputMethodManager imeManager = (InputMethodManager) mw.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imeManager != null) {
    imeManager.showInputMethodPicker();
} else {
    Toast.makeText(mw.getApplicationContext(), "IME ERROR",
    Toast.LENGTH_LONG).show();
}
È stato utile?

Soluzione

Unfortunately you cannot catch the input method which user picks in InputMethodPicker.

However, you can check it after user picks it, using BroadcastReceiver.
When IME changes, Intent.ACTION_INPUT_METHOD_CHANGED will be broadcasted.

public class InputMethodChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_INPUT_METHOD_CHANGED)) {
            .....
            /* You can check the package name of current IME here.*/
        }
    }
}

Then, register it.

IntentFilter filter = new IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED);
registerReceiver(mReceiver, filter);

Altri suggerimenti

Well, even though you accepted the answer, there is a way to check if user selects your keyboard as a default:

public static boolean isThisKeyboardSetAsDefaultIME(Context context) {
    final String defaultIME = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
    return isThisKeyboardSetAsDefaultIME(defaultIME, context.getPackageName());
}

public static boolean isThisKeyboardSetAsDefaultIME(String defaultIME, String myPackageName) {
    if (TextUtils.isEmpty(defaultIME))
        return false;

    ComponentName defaultInputMethod = ComponentName.unflattenFromString(defaultIME);
    if (defaultInputMethod.getPackageName().equals(myPackageName)) {
        return true;
    } else {
        return false;
    }
}

You can combine this code with code from this answer and once user selects keyboard from InputMethodPicker check if your keyboard is a default (meaning that user selected it).

If you have more questions about implementing a keyboard, check this project. Cheers.

i am doing this way to detect if my ime is selected or enabled

for checking isSelected

 public static boolean isThisKeyboardSelected(Context context) {
        final String defaultIME = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
        if (TextUtils.isEmpty(defaultIME))
            return false;
        ComponentName defaultInputMethod = ComponentName.unflattenFromString(defaultIME);
//        return defaultInputMethod.getPackageName().equals(myPackageName);
        return defaultIME.contains("your package Name here");

    }



     public static boolean isThisKeyboardSetAsDefaultIME(Context context) {
        final String defaultIME = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_INPUT_METHODS);
        if (TextUtils.isEmpty(defaultIME))
            return false;
        ComponentName defaultInputMethod = ComponentName.unflattenFromString(defaultIME);
//        return defaultInputMethod.getPackageName().equals(myPackageName);
        return defaultIME.contains("your package name);

    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top