简体   繁体   中英

How can I get a list of all the Input Methods (and their names) that are installed on the phone?

I'm trying to display a list of all the Input Methods that are currently installed on the phone. I'm getting a list of InputMethodInfo objects by doing this:

InputMethodManager imeManager = (InputMethodManager)getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
List<InputMethodInfo> InputMethods = imeManager.getEnabledInputMethodList();

This works, but the InputMethodInfo object doesn't have a friendly name on it anywhere. For example, instead of "Swype" it will give me "com.swype.android.inputmethod.SwypeInputMethod"

That isn't a very friendly way to display the list to the user, and these package names don't follow a strict pattern so there's no way for me to reliably parse the keyboard name out of the class name.

I even tried to get really fancy and get the InputMethodInfo's corresponding ServiceInfo object so I could resolve its Label Resource integer, but I just get NameNotFoundExceptions every time this runs.

ComponentName componentName = inputMethodInfo.getComponent();
ServiceInfo serviceInfo = packageManager.getServiceInfo(componentName, 0);
Resources resources = getResources();
try 
{
 String imeServiceLabel = resources.getString(serviceInfo.labelRes);
} 
catch (NameNotFoundException e) { }

Does anybody know how to accomplish this? I don't care how I have to do it, I just need to be able to produce a list of Input Methods as they appear in the phone's Language and Keyboards menu and then store the user's selection. I thought maybe I could just use the InputMethodManager to launch the standard Input Method selection menu and then see which one the user picked by looking at which IME is currently selected after the menu closes, but as far as I can tell there's no way to see which IME is currently selected in the system.

I wasted a lot of time on this when it was right under my nose. I discovered the answer to my own question while I was doing something else. This will give you the friendly name of an Input Method through the regular InputMethodInfo object.

inputMethodInfo.loadLabel(packageManager).toString();

I'm too used to writing C# where everything is implemented as a Property, so I did most of my investigation by looking through object properties at run time. Java, however, offers most of this type of information through public methods, and I just happened to notice it when the auto-complete box popped up in Eclipse.

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