闲话不多说,直接上代码
import android.app.ActivityManagerNative;
import android.os.UserHandle;
import android.os.RemoteException;
import android.provider.Settings;
String imid = “com.android.inputmethod.latin/.LatinIME”;//输入法的包名
public static void setInputMethod(String imid,Context context) {
int userId;
try {
userId = ActivityManagerNative.getDefault().getCurrentUser().id;
Settings.Secure.putStringForUser(context.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD, imid, userId);
if (ActivityManagerNative.isSystemReady()) {
Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
intent.putExtra("input_method_id", imid);
context.sendBroadcastAsUser(intent, UserHandle.CURRENT);
}
} catch (RemoteException e) {
}
}