Here are the before and after screen shot(s).


We can use IWindowManager’s injectKeyEvent to simulate a series of key strokes
package org.apache.hello;
import android.app.Activity;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.ServiceManager;
import android.util.Log;
import android.view.IWindowManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
public class HelloApp extends Activity {
final IWindowManager windowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.setContentView(R.layout.main);
Button fill = (Button) this.findViewById(R.id.fill);
fill.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Thread t = new Thread(new Runnable() {
public void run() {
try {
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A));
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A));
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_N));
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_N));
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_D));
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_D));
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_R));
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_R));
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_O));
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_O));
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_I));
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_I));
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_D));
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_D));
} catch (Exception e) {
Log.e("HelloApp", e.getMessage(), e);
}
}
});
t.start();
}
});
}
public void sendKeySync(KeyEvent event) throws DeadObjectException {
windowManager.injectKeyEvent(event.isDown(), event.getKeyCode(), event.getRepeatCount(), event.getDownTime(), event.getEventTime(), true);
}
}
Notes
Like this:
Like Loading...
I am not able to download SimulateKeyStrokes.zip. Can you please post the new link to the same.
Thanks.
Comment by Savita Singh — January 22, 2009 @ 6:54 pm
Is it possible to post the key strokes form background app?
Comment by alsher — September 24, 2009 @ 1:43 pm
Hi, i cudn’t find the IWindowManager class .. and i m getting import error.
Please help .. how to do..?
Comment by Anand — July 9, 2010 @ 6:41 am
This app is a fake IWindowManager not exist.
Comment by A — August 23, 2011 @ 10:45 am