1. 程式人生 > >android通過Instrumentation來模擬鍵盤點選操作

android通過Instrumentation來模擬鍵盤點選操作

android.app.Instrumentation好像原來是用來做測試的,

可以用來模擬很多activity的操作

主要程式碼如下

package com.qefee.testinstrumentation;

import android.app.Activity;
import android.app.Instrumentation;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	TextView tv_showkey;
	Button btn_submit;
	EditText et_keycode;
	Instrumentation instrumentation;

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

		instrumentation = new Instrumentation();
		getViews();

		// 通過按鈕點選,模擬事件
		btn_submit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				et_keycode.clearFocus();
				btn_submit.requestFocus();
				String keycodeStr = et_keycode.getText().toString();

				if (keycodeStr == null || "".equals(keycodeStr)) {
					Toast.makeText(MainActivity.this, "please input a keycode",
							Toast.LENGTH_SHORT).show();
					return;
				}

				final int keycode = Integer.parseInt(keycodeStr);

				// 必需線上程中執行,否者報錯
				Thread t = new Thread(new Runnable() {

					@Override
					public void run() {
						instrumentation.sendKeyDownUpSync(keycode);
					}
				});
				t.start();

			}
		});
	}

	/**
	 * 
	 */
	private void getViews() {
		tv_showkey = (TextView) findViewById(R.id.tv_showcode);
		btn_submit = (Button) findViewById(R.id.btn_submit);
		et_keycode = (EditText) findViewById(R.id.et_keycode);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean onKeyDown = super.onKeyDown(keyCode, event);
		tv_showkey.setText("des = " + KeyEvent.keyCodeToString(keyCode)
				+ " : code = " + keyCode);
		System.out.println(event);
		// System.out.println(event.getAction());
		// System.out.println(event.getCharacters());
		// System.out.println(event.getDeviceId());
		// System.out.println(event.getDisplayLabel());
		// System.out.println(event.getDownTime());
		// System.out.println(event.getEventTime());
		// System.out.println(event.getFlags());
		// System.out.println(event.getKeyCode());
		//
		// System.out.println(KeyEvent.keyCodeToString(keyCode));

		return onKeyDown;
	}
}

如果在文字框中輸入24,或者25

點選按鈕就能模擬音量加減鍵

鍵值可以檢視android.view.KeyEvent