在开发中想用代码模拟其他物理按键输入,这里用了shell 命令在程序运行时环境里模拟按键事件。
private int volume = 0;private TimerTask timerTask;private Timer timer = new Timer();private SharedPreferences sp;private SharedPreferences.Editor editor;//模拟按键加音量,按六次
//因命令执行会阻塞线程, 所以在子线程里执行private void startRun() {timerTask = new TimerTask() {@Overridepublic void run() {Message message = Message.obtain();message.what = 6000;mHandler.sendMessage(message);}};timer.schedule(timerTask, 0, 1);}Handler mHandler = new Handler() {public void handleMessage(Message paramMessage) {switch (paramMessage.what) {case 6000:volume = sharedPreferences.getInt("volume", 0);if (volume < 6) {//模拟按键加音量