Android 获取当前系统输入法和修改系统输入法

最近做盒子开发中,有需求要修改输入法的功能,查阅资料,终于解决了这个问题,现在把代码贴出来:

import android.app.Dialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.inputmethodservice.InputMethodService;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xinrui.tvsetting.R;
import com.xinrui.tvsetting.common.ResultCallbackListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class InputMethodDialog extends Dialog implements View.OnClickListener{
    private ResultCallbackListener callbackListener;
    private RadioGroup inputmethod_type;
    private RadioButton google_rdb,sogou_rdb;
    private Context mContext;
    private Button ok_btn,cancle_btn;
    private String inputmethod="";
    private List<InputMethodInfo> methodList;
    private Map<String,String> inputmap = new HashMap<>();
    private PackageManager pm;
    private ApplicationInfo appinfo;
    public InputMethodDialog(Context context, int theme) {
        super(context, theme);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inputmethod_layout);
        initView();
    }

    public void setCallbackListener(ResultCallbackListener listener){
        this.callbackListener = listener;
    }

    private void initView(){
       inputmethod_type = (RadioGroup)findViewById(R.id.inputmethod_type);
       google_rdb=(RadioButton)findViewById(R.id.google_type);
       sogou_rdb=(RadioButton)findViewById(R.id.sougou_type);
       ok_btn=(Button)findViewById(R.id.input_ok);
       cancle_btn=(Button)findViewById(R.id.input_cancle);
       ok_btn.setOnClickListener(this);
       cancle_btn.setOnClickListener(this);
       getInputMethodList();
//        final InputMethodService service = new InputMethodService();
       inputmethod_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup group, int checkedId) {
               switch (checkedId){
                   case R.id.google_type:
                       inputmethod="谷歌拼音输入法";
//                       service.switchInputMethod(inputmap.get(inputmethod));
                       Settings.Secure.putString(mContext.getContentResolver(),
                               Settings.Secure.DEFAULT_INPUT_METHOD,"com.android.inputmethod.pinyin/.PinyinIME");
                       break;
                   case R.id.sougou_type:
                       inputmethod="搜狗输入法";
                       Settings.Secure.putString(mContext.getContentResolver(),
                               Settings.Secure.DEFAULT_INPUT_METHOD,"com.sohu.inputmethod.sogouoem/.SogouIME");
                       break;
               }
           }
       });
       String curinputName = getcurinputname();
       if(curinputName.equals("谷歌拼音输入法")){
           inputmethod_type.check(R.id.google_type);
       }else{
           inputmethod_type.check(R.id.sougou_type);
       }
    }

    private String getcurinputname() {
        String package_name = getDefaultInputMethodPkgName(mContext);
        pm = mContext.getPackageManager();
        try {
            appinfo = pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pm.getApplicationLabel(appinfo).toString();
    }

    //获取默认输入法包名:
    private String getDefaultInputMethodPkgName(Context context) {
        String mDefaultInputMethodPkg = null;

        String mDefaultInputMethodCls = Settings.Secure.getString(
                context.getContentResolver(),
                Settings.Secure.DEFAULT_INPUT_METHOD);
        //输入法类名信息
        //Log.e(TAG, "mDefaultInputMethodCls=" + mDefaultInputMethodCls);
        if (!TextUtils.isEmpty(mDefaultInputMethodCls)) {
            //输入法包名
            mDefaultInputMethodPkg = mDefaultInputMethodCls.split("/")[0];
            //Log.e(TAG, "mDefaultInputMethodPkg=" + mDefaultInputMethodPkg);
        }
        return mDefaultInputMethodPkg;
    }

    public void getInputMethodList(){
        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        methodList = imm.getInputMethodList();
        for(int i=0;i<methodList.size();i++) {
            InputMethodInfo mi = methodList.get(i);
            String name = mi.loadLabel(mContext.getPackageManager()).toString();
            String id = mi.getId();
//            Log.e("MainActivity","id:"+id+"--name:"+name);
            if(id.equals("com.android.inputmethod.pinyin/.PinyinIME")){
                if(i==0) google_rdb.setChecked(true);
                google_rdb.setText(name);
            } else if(id.equals("com.sohu.inputmethod.sogouoem/.SogouIME")){
                if(i==0) sogou_rdb.setChecked(true);
                sogou_rdb.setText(name);
            }
            inputmap.put(name,id);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.input_ok:
                if(callbackListener!=null&&!TextUtils.isEmpty(inputmethod)) {
                    callbackListener.getValue(inputmethod);
                }
                dismiss();
                break;
            case R.id.input_cancle:
                dismiss();
                break;
        }
    }
}

通过Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD,“com.sohu.inputmethod.sogouoem/.SogouIME”);//第三个参数就是 输入法id了 不过需要系统权限
这样就可以切换输入法了

你可能感兴趣的:(android)