How to Simulate Touch from Background Service with Sendevent or Other Way

How to simulate touch from background service with sendevent or other way?

I can't execute the "sendevent" command, but found another way for myself, hope it will be helpfull for somebody.

For simulate touch I used sendPointerSync() from android.app.Instrumentation, that work only with "android.permission.INJECT_EVENTS" permission. For use it you should compile your app as a system app.
To do it you should follow next steps:

  1. Getting files from android source:

    root-of-android-source-tree/out/host//framework/signapk.jar

    root-of-android-source-tree/build/target/product/security/platform.x509.pem

    root-of-android-source-tree/build/target/product/security/platform.pk8

  2. sign your app using getting files:

    Command "java -jar signapk.jar platform.x509.pem platform.pk8 YourApp-unsigned.apk" YourApp-signed.apk.

  3. adb install YourApp-signed.apk

    • Run your app
    • Use "adb shell ps" to confirm that your app is running as system.

Code with touch simulating(new thread is necessary for simulation):

Thread thread = new Thread(){
@Override
public void run(){
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,width*4/5,height, 0));
}
};

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp"
**android:sharedUserId="android.uid.system"**
android:versionCode="1"
android:versionName="1.0" >


Using resources:

  • Programmatically Injecting Events on Android – Part 1
  • How to compile Android Application with system permissions
  • Instruction for compile here

Can I programatically generate a touch event in Android from background app on keypress?

Apps cannot generate arbitrary touch events, except perhaps using superuser privileges on rooted devices. You are welcome to use the accessibility framework to create an AccessibilityService, but its input options are very limited.

Send touch screen event to foreground activity (only from APK)

There appears to be something new in Jelly Bean! The input commands have improved:

usage: input ...
input text <string>
input keyevent <key code number or name>
-> input tap <x> <y>
-> input swipe <x1> <y1> <x2> <y2>

It worked fine on 4.1.2

Invoking sendevent in android via code

"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();

Android self-signed MotionEvent APK

A few things:

Verify that your APK is signed correctly:

jarsigner -verify -verbose -certs app-signed.apk

Then use adb install -r app-signed.apk to replace the existing application.

See this guide for additional info on App signing.

Also preferably you should use the official Command Line Tools.

(I don't see the benefit in using the github project you were referring to and it also seems to be abandoned.)

How to simulate screen touch on android with xamarin?

You can also Do this without root access. Sorry for the code because I don't have xamrin experience. however you can implement accessibility service to simulate button,textview...... clicks. you can check Grenify application for the example. Greenify app opens app info programitically and clicks on force close button programitically. even without root access
Here is the application link.
https://play.google.com/store/apps/details?id=com.oasisfeng.greenify&hl=en

Note: I have also read several posts which tell that it is not possible but I don't belive after using Greenify applicaction.

My Own Research about accessibility services (To monitor whatsapp messages for parental spy application project):
using accessibility service I have monitored WhatsApp messages from thier textView.getText() function call.
I can share My Android code for Help If needed.
Thanks,
Jawad Zeb



Related Topics



Leave a reply



Submit