Skip to content Skip to sidebar Skip to footer

Android: Help Creating A Button That Produces The Same Result As Hitting The Down Key On The D-pad? (part 2)

Why doesn't this work?? I am trying to create an onClickListener for a button that produces the same effect as pressing the 'down' key on the D-pad. Eclipse gives me an error, sayi

Solution 1:

You're trying to invoke non-static method in a static way. You need to obtain an instance of the service first and then invoke the method on the instance. Also the way you're doing the simulation of keypress looks incorrect. UPD: After some digging I've managed to simulate key event, try:

new Thread(new Runnable() {         
    @Override
    public void run() {                 
        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
    }   
}).start();

Solution 2:

Same solution, just will take a parameter.

privatevoidInjectKeys(finalint keyEventCode) {
 newThread(newRunnable() {
  @Overridepublicvoidrun() {
   newInstrumentation().sendKeyDownUpSync(keyEventCode);
  }
 }).start();
}

Just call and pass the KeyEvent.KEYCODE like so InjectKeys(KeyEvent.KEYCODE_DPAD_DOWN);

Please don't downvote or upvote me, my answer is exactly like the above answer, I just modified it to use parameters.

Post a Comment for "Android: Help Creating A Button That Produces The Same Result As Hitting The Down Key On The D-pad? (part 2)"