android 检测外接键盘并设置输入法布局

需求:

接入外接键盘后, 系统输入语言自动变更为法语键盘

 

实现:

监听外接设备变更 > 查询是否包含全键盘设备 > 变更输入法布局

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

<!-- 开机自动启动 -->
<receiver android:name=".Receiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
			<intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.DEFAULT"/> 
            </intent-filter>
        </receiver>
<!-- 监听服务 -->
		<service android:name=".KeyboardService">
            <intent-filter>
				<action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/> 
            </intent-filter>
        </service>

 开机启动服务:

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.zed.keyboardDeviceReceiver.*;

public class Receiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Intent i = new Intent(context, KeyboardService.class);
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startService(i);
	}


    
}

 

import android.app.Service;
import android.os.IBinder;
import android.content.Context;
import android.view.InputDevice;
import android.os.Handler;
import android.content.Intent;
import android.os.Message;
import android.content.res.Configuration;
import android.hardware.input.InputManager;

public class KeyboardService extends Service implements InputManager.InputDeviceListener{
	private InputManager mIm;
	private Context context;
	public void onCreate(){
		super.onCreate();
		context = getApplicationContext();
	}
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		 mIm = (InputManager)context.getSystemService(Context.INPUT_SERVICE);
		 mIm.registerInputDeviceListener(this, null);//添加监听
		int[] ids = mIm.getInputDeviceIds();//非常重要, 决定后续是否能监听到状态变化
	}
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		//doNothing
android.util.Log.d("KeyboardService", "0410 onConfigurationChanged");
		
	}
	public void onDestroy(){
		super.onDestroy();
		mIm.unregisterInputDeviceListener(this);//注销监听
	}
	public IBinder onBind(android.content.Intent intent){
		return null;
	}

    public void onInputDeviceAdded(int deviceId){
android.util.Log.d("KeyboardService", "0410 > onInputDeviceAdded()");
		final int[] devices = InputDevice.getDeviceIds();//获取设备
		for (int i = 0; i < devices.length; i++) {//遍历查找
        	InputDevice device = InputDevice.getDevice(devices[i]);
                if (device != null
                        && !device.isVirtual()
                        && device.isFullKeyboard()) {
                    final String inputDeviceDescriptor = device.getDescriptor();
                    final String keyboardLayoutDescriptor =
                            mIm.getCurrentKeyboardLayoutForInputDevice(inputDeviceDescriptor);
                    mIm.addKeyboardLayoutForInputDevice(inputDeviceDescriptor,
                            "com.android.inputdevices/com.android.inputdevices.InputDeviceReceiver/keyboard_layout_french");//添加布局
					mIm.setCurrentKeyboardLayoutForInputDevice(inputDeviceDescriptor,
                        "com.android.inputdevices/com.android.inputdevices.InputDeviceReceiver/keyboard_layout_french");//更改布局
                }
            }
	}

    public void onInputDeviceRemoved(int deviceId){
		android.util.Log.d("KeyboardService", "0410 > onInputDeviceRemoved()");
	}

    public void onInputDeviceChanged(int deviceId){
		android.util.Log.d("KeyboardService", "0410 > onInputDeviceChanged()");
	}

}

 

 

 

你可能感兴趣的:(keyboard,inputmethod,InputDevice)