How can I simulate a touch screen tap on android?

14,707 views
Skip to first unread message

Dhanesh

unread,
Dec 2, 2010, 4:12:08 AM12/2/10
to android-platform
Hi,

I would like to simulate a mouse pointer(touch screen tap) in my
android ?

The concept is as follows:
There will have an application which sends the (x,y) coordinates to a
running daemon. The daemon should be able generate(simulate) a tap at
the corresponding
point on the touch screen.

Has anyone done such kind of developments? Which part of android
codebase I will have to look into?

Any inputs on this will be greatly appreciated.

Regards,
Dhanesh

Mike Playle

unread,
Dec 2, 2010, 11:12:05 AM12/2/10
to android-...@googlegroups.com
On Thu, 2010-12-02 at 01:12 -0800, Dhanesh wrote:
> Hi,
>
> I would like to simulate a mouse pointer(touch screen tap) in my
> android ?

If you're injecting into your own application, there's a method
'injectPointerEvent()' in android.view.IWindowManager that might
do what you want.

I don't think you can use it to inject into other processes though.

Mike


albert jordan

unread,
Dec 2, 2010, 1:16:44 PM12/2/10
to android-...@googlegroups.com

It depends what you are trying to do.

I used Robotium, which is a testing framework for Android, to generate touch events to do a range of things from testing to automated provisioning of the app.

http://code.google.com/p/robotium/wiki/Getting_Started


Albert

> --
> You received this message because you are subscribed to the Google Groups "android-platform" group.
> To post to this group, send email to android-...@googlegroups.com.
> To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.
>

Dhanesh

unread,
Dec 3, 2010, 4:28:29 AM12/3/10
to android-platform
Hi Mike,
Thanks for the answer.

I actually wanted to inject it to all the applications. Or more
specifically to the application in focus.
Can I do something in the framework source to achieve this ?


Btw, I tried to use the injectPointerEvent as u said,
But it showed "import android.view.IWindowManager cannot be resolved"
error..

Any Idea?

Machiry

unread,
Oct 20, 2012, 2:05:58 PM10/20/12
to android-...@googlegroups.com
Monkey does exactly what you want.

you can refer its sources in your android enlistment:@ development/cmds/monkey/src/com/android/commands/monkey

The exact code that injects using WindowManager is @: development/cmds/monkey/src/com/android/commands/monkey/MonkeyMotionEvent.java

-Machiry

Emrah Yilmaz

unread,
Oct 23, 2012, 9:10:11 AM10/23/12
to android-...@googlegroups.com
Hi Machiry,

Is there a sample that i can follow? It looks complicated.

BR

Emrah Yilmaz

unread,
Oct 23, 2012, 9:14:15 AM10/23/12
to android-...@googlegroups.com
By the way it does it from adb, i need to generate touch events from inside the applicaiton.



On Saturday, October 20, 2012 9:05:58 PM UTC+3, Machiry wrote:

Machiry

unread,
Oct 23, 2012, 10:52:23 AM10/23/12
to android-...@googlegroups.com
adb is just used to launch monkey.

as mentioned earlier : Monkey uses WindowManager to inject the event.

Complete and simple example refer : frameworks/base/cmds/input/src/com/android/commands/input/Input.java


     Note: for meta sate you can set it as 0 , unless you have to inject along with modifiers: http://developer.android.com/reference/android/view/MotionEvent.html#getMetaState()

Sample Code:

 IWindowManager mWm = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
  //MotionEvent m = MotionEvent.obtain() //Refer the link above
   mWm.injectPointerEvent(m, false);
Reply all
Reply to author
Forward
0 new messages