Code example for Instrumentation

Methods: sendKeyDownUpSync

0
     
    private void press_key (final int keyCmd) {
    	Thread myThread = new Thread() {
            public void run() { 
    	    	try { 
    	    		Instrumentation inst = new Instrumentation();
    		        inst.sendKeyDownUpSync(keyCmd);
    		        Log.v(TAG, "key "+keyCmd+" pressed");
    	    	} catch (SecurityException e) {
    	    		Log.e(TAG, "Injecting Keys to other application is impossible due to non-system-permission");
    	    	} catch (Exception e) {
    	    		Log.e(TAG, "error while simulating key event :\n", e);
    	    	} 
    	    } 
    	}; 
    	myThread.start();
    } 
 
	private void press_key_corresponding_to_string (String msg) {
		for (char ch: msg.toCharArray()) {
			switch(ch) {