Switch keyboard profile programmatically

廉价感情. 提交于 2019-11-26 07:34:32

问题


Is there any way that we can switch installed keyboards programmatically (without going to the settings section manually)?

My requirement is that the user is presented with all the keyboards which are installed on the phone and gets a chooser dialog to switch to the one wishes?

(basically we want to cut down the step to transfer him to the settings page)


回答1:


This piece of code will fulfill your requirements:

InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();

As Commonsware points out in his answer, there is no way to do this behind the user's back.




回答2:


If your app has system privileges, and has the permission

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

you can programatically enable the keyboard and set it as the current keyboard by making it the default keyboard WITHOUT USER KNOWLEDGE OR INTERVENTION!

//get the old default keyboard in case you want to use it later, or keep it enabled
String oldDefaultKeyboard = Settings.Secure.getString(resolver, Setting.Secure.DEFAULT_INPUT_METHOD);

//enable your keyboard
Settings.Secure.putString(resolver, Settings.Secure.ENABLED_INPUT_METHODS, "com.my.keyboard/.full.path");

//set your keyboard as the new default keyboard
Settings.Secure.putString(resolver, Settings.Secure.DEFAULT_INPUT_METHOD, "com.my.keyboard/.full.path");

You can enable multiple keyboards (such as the default keyboard and your own) by providing a list of keyboards to the ENABLED_INPUT_METHODS, separated by ':'. See docs

You can verify your keyboard's full package and path ID by invoking ime list -a through adb shell




回答3:


If you have rooted device, you can use /system/bin/ime utility.

List all installed input methods: # ime list -a

Set google's keyboard as default:
# ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME

On Java side use Runtime.getRuntime().exec(...).




回答4:


Is there any way that we can switch installed keyboards programmatically (without going to the settings section)?

Fortunately, no, for security reasons. If an app could dictate what input method editor is used, malware would change the input method editor to their keylogger.




回答5:


    InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
    imeManager.showInputMethodPicker();

This code will prompt user to change default keyboard




回答6:


import android.content.Intent;

import android.view.inputmethod.InputMethodManager;

// To enable keyboard

startActivity(new Intent("android.settings.INPUT_METHOD_SETTINGS"));

// To activate the keyboard

InputMethodManager imeManager = (InputMethodManager) 
getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();


来源:https://stackoverflow.com/questions/11036435/switch-keyboard-profile-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!