3
    adb shell sendevent /dev/input/event0 3 0 45
    adb shell sendevent /dev/input/event0 3 1 784
    adb shell sendevent /dev/input/event0 1 330 1
    adb shell sendevent /dev/input/event0 0 0 0
    adb shell sendevent /dev/input/event0 1 330 0
    adb shell sendevent /dev/input/event0 0 0 0

Is there any ways to invoke sendevent through android code? When I executed above commands from shell, I am able to generate click event in the emulator. Is it possible to generate same effect from android code? Any one please help me.

user1752763
  • 33
  • 1
  • 5

3 Answers3

1

"sendevent" command for click (or touch) event requires "root" or "su" permission.

I tried to solve same problem in this question, but I not found any solution for simulate touch in this way or any other way using only SDK tools.

However there are another ways to simulate touch, described in this article .

For myself work next (app signed and run as a system application):

Thread thread = new Thread(){
@Override
public void run(){
    try {
        this.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Instrumentation m_Instrumentation = new Instrumentation();
        m_Instrumentation.sendPointerSync(MotionEvent.obtain(
                SystemClock.uptimeMillis(),
                SystemClock.uptimeMillis(),
            MotionEvent.ACTION_DOWN,posx, posy, 0));
    m_Instrumentation.sendPointerSync(MotionEvent.obtain(
            SystemClock.uptimeMillis(),
            SystemClock.uptimeMillis(),
            MotionEvent.ACTION_UP,posx, posy, 0));
     }
};
thread.start();
Community
  • 1
  • 1
v1k
  • 1,307
  • 2
  • 15
  • 20
1

I don't know where to put this but if anyone is having trouble getting the 'sendevent' code to work on ICS using the method first posted here I've figured out how to do it. It does require SU access though. The problem for me at least was this.

I'll post the code below and then explain each part.

When you do 'getevent' to get the hex code for your touch events on your touchpad (mine was /dev/event1) it spits out a bunch of data that after converting from hex to dec will look like this.

The first group is 7 lines and the next is 9 for a single touch event. Why? no clue. The syntax is this (device) (1, 3, or 0, not understood) (value, which can mean many things. 58 in my case was an identifier for my device, 53 and 54 denote that coordinates are the value) (value, etc)

sendevent /dev/input/event1    3    58   255 #means '3, variable 58 is 255'
sendevent /dev/input/event1    3    53   534 #'3, variable 53 (x axis) is 534
sendevent /dev/input/event1    3    54   321 #'3, variable 54 (y axis) is 534
sendevent /dev/input/event1    3    48     8 #'3, variable 48 (pressure maybe) is 8
sendevent /dev/input/event1    0     2     0 #i think the 0,2,0 means 'finger down or up
sendevent /dev/input/event1    1   330     1 #this, followed by 0,0,0 means 'end of event'
sendevent /dev/input/event1    0     0     0

same with all this, accept this is the other end of the event, if the first part meant #'he put his finger down' this means 'he picked it up.

sendevent /dev/input/event1    3    58     0
sendevent /dev/input/event1    3    53   534
sendevent /dev/input/event1    3    54   321
sendevent /dev/input/event1    3    48     8
sendevent /dev/input/event1    0     2     0 
sendevent /dev/input/event1    0     0     0
sendevent /dev/input/event1    1   330     0
sendevent /dev/input/event1    0     2     0
sendevent /dev/input/event1    0     0     0

at first glance this will appear to be two different touch events which is where i went wrong. It's actually a 'call and answer type of situation.' this code will produce a single touch event. by the way i used a shell script and smanager to get this without having to decipher the hex data myself it was called 'sendeventgetevent.sh' google it.

I'm high which makes it hard to explain but the following code if you're root will produce a single touch event on the coordinates provided in 4.0.4 ICS. You'll have to tweak it to get it right of course. download sendeventgetevent.sh, use a script manager like smanager to run it and rock on.

$su
sendevent /dev/input/event1    3    58   255;
sendevent /dev/input/event1    3    53   534;
sendevent /dev/input/event1    3    54   321;
sendevent /dev/input/event1    3    48     8;
sendevent /dev/input/event1    0     2     0;
sendevent /dev/input/event1    1   330     1;
sendevent /dev/input/event1    0     0     0;
sendevent /dev/input/event1    3    58     0;
sendevent /dev/input/event1    3    53   534;
sendevent /dev/input/event1    3    54   321;
sendevent /dev/input/event1    3    48     8;
sendevent /dev/input/event1    0     2     0;
sendevent /dev/input/event1    0     0     0;
sendevent /dev/input/event1    1   330     0;
sendevent /dev/input/event1    0     2     0;
sendevent /dev/input/event1    0     0     0;
Regular Jo
  • 5,190
  • 3
  • 25
  • 47
John
  • 11
  • 1
0

You should be able to issue the same shell command via the Runtime.exec() method: http://developer.android.com/reference/java/lang/Runtime.html

Cesar Maiorino
  • 563
  • 5
  • 16