android切换输入法工具类
共享一个切换输入法的工具类,可以实现以下功能
- 检测输入法是否安装
- 安装输入法
- 切换输入法(在utf7Ime和android之间切换,如果需要切换成其他的输入法,请自行修改)
import android.util.Log;
import com.meitu.travelapk.data.ConfigData;
import com.meitu.travelapk.data.PathData;
import com.meitu.travelapk.data.TestReport;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
/**
* 用于输入法的管理
*/
public class InputMethodUtils {
private static final String TAG = "InputMethodUtils";
public static String UTF7_PKG = "jp.jun_nama.test.utf7ime";
public static String UTF7_KEY = "jp.jun_nama.test.utf7ime/.Utf7ImeService";
public static String Android_KEY = "com.android.inputmethod.latin/.LatinIME";
public enum InputMethod {
UTF7, ANDROID
}
/**
* 安装utf7输入法
*/
public static void installUTF7() {
if (!DeviceUtils.isAppInstalled(UTF7_PKG)) {
//拷贝apk到指定目录
String path = ConfigData.rootPath;
String fileName = PathData.utfApkName;
File apkFile = new File(path + fileName);
if (!apkFile.exists()) {
FileUtils.copyAssets(path, fileName);
}
//安装
DeviceUtils.installApk(apkFile);
}
}
/**
* 切换将当前输入法,另启一个线程
*/
public static void switchInputMethod(final InputMethod input) {
Thread switchThread = new Thread(new Runnable() {
@Override
public void run() {
String installCommand;
switch (input) {
case UTF7:
if (!DeviceUtils.isAppInstalled(UTF7_PKG)) {
installUTF7();
}
installCommand = "settings put secure default_input_method " + UTF7_KEY;
TestReport.i("切换到输入法[utf7]");
break;
case ANDROID:
installCommand = "settings put secure default_input_method " + Android_KEY;
TestReport.i("切换到输入法[Android]");
break;
default:
installCommand = "settings put secure default_input_method " + Android_KEY;
break;
}
ShellUtils.execCommand(installCommand, false);//这里你也可以使用Runtime.getRunteim的方法去执行
}
});
switchThread.start();
}
}