// First start the activity we are instrumenting -- the contacts
Intent intent = new Intent(Intent.MAIN_ACTION);
Activity activity = startActivitySync(intent);
// This is the Activity object that was started, to do with as we want.
Log.i("ContactsFilterInstrumentation", "Started: " + activity);
// We are going to enqueue a couple key events to simulate the user
// filtering the list. This is the low-level API so we must send both
// down and up events.
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_M));
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_M));
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A));
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A));
// Wait for the activity to finish all of its processing.
// And we are done!