How to send dispatchTouchEvent () from a service to any Underlay operation

WHAT DO I WANT TO DO? - Basically, I want to pragmatically make human contact at certain times with certain X, Y coordinates - Run a service that receives X, Y coordinates through some interface (TCP / UDP) - fire dispatchTouchEvent (), using these coordinates and getting the results, since an actual click of a person’s finger gives - I mean, the focal activity can be anything, either this is the Android Home screen or any open activity OR Anything - The moment service calls the dispatchTouchEvent () method, which should execute the click. - For example, if the open application is a calculator and the X, Y coordinates of dispatchTouchEvent () indicate a numeric value of 5, it should press the 5 key

WHAT DO I DO NOW? - I started a service that gets the X, Y coordinates - On some trigger, it calls dispatchTouchEvent (), but it does not work. He does not click on the screen. - It captures every touch event made by a human finger.

WHAT'S HAPPENING? - How to throw dispatchTouchEvent () for any pad action

SAMPLE CODE: Here I use the sample code. I just made it easy to test. First we need to click anywhere on the screen. Its X, Y coordinates will be saved and after a while dispatchTouchEvent () will be called using the same coordinates.

MainActivity.java

package com.test.overlay; import android.os.Bundle; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Intent intent = new Intent(this, Overlay.class); Intent intent = new Intent(this, PlayerServiceTest.class); startService(intent); finish(); } } 

Overlay.java

 package com.test.overlay; import android.annotation.SuppressLint; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Typeface; import android.os.AsyncTask; import android.os.Build; import android.os.IBinder; import android.os.SystemClock; import android.view.Gravity; import android.view.MotionEvent; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Toast; @SuppressWarnings("deprecation") public class Overlay extends Service { HUDView mView; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show(); mView = new HUDView(this); /* WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, //WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, //WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); */ WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); /* WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, //WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT); */ params.gravity = Gravity.RIGHT | Gravity.TOP; params.setTitle("Load Average"); WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.addView(mView, params); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show(); if(mView != null) { ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView); mView = null; } } } class HUDView extends ViewGroup { private Paint mLoadPaint; public HUDView(Context context) { super(context); Toast.makeText(getContext(),"HUDView", Toast.LENGTH_LONG).show(); mLoadPaint = new Paint(); mLoadPaint.setAntiAlias(true); //mLoadPaint.setTextSize(10); mLoadPaint.setTextSize(25); //mLoadPaint.setTypeface(Typeface.SANS_SERIF); mLoadPaint.setTypeface(Typeface.DEFAULT_BOLD); mLoadPaint.setARGB(255, 25, 200, 50); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawText("THIS IS TEST", 15, 115, mLoadPaint); } @Override protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) { System.out.println("onLayout called ..."); //handleAutoTouches(); } //=============================================================================== @SuppressLint("NewApi") public void TestThread() { AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { @Override protected void onPreExecute() { //System.out.println("post0..."); } @Override protected Void doInBackground(Void... arg0) { //System.out.println("Time started-1"); try { //if(flag != 0) Thread.sleep(7*1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { Toast.makeText(getContext(), "pressed", Toast.LENGTH_SHORT).show(); //System.out.println("handleAutoTouches called-2"); handleAutoTouches(); } }; if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null); else task.execute((Void[])null); } //======================================================================================= public void handleAutoTouches() { long downTime = SystemClock.uptimeMillis(); //long eventTime = SystemClock.uptimeMillis() + 100; long eventTime = SystemClock.uptimeMillis()+700; //float x = 71.0f; //float y = 138.0f; //float x = 120.0f; //float y = 170.0f; //Set Manual x/y co-rdinates here float x = xCor; float y = yCor; boolean val; // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState() int metaState = 0; final MotionEvent motionEvent = MotionEvent.obtain( downTime, eventTime, //MotionEvent.ACTION_UP, MotionEvent.ACTION_DOWN, x, y, metaState ); System.out.println("CLICKED DOWN..."); val = super.dispatchTouchEvent(motionEvent); System.out.println("Status:"+val); final MotionEvent motionEvent1 = MotionEvent.obtain( downTime, eventTime, //MotionEvent.ACTION_UP, MotionEvent.ACTION_UP, x, y, metaState ); System.out.println("CLICKED UP..."); val = super.dispatchTouchEvent(motionEvent1); System.out.println("Status:"+val); //------------------------------------------------------- flag=0; } int flag=0; float xCor=0; float yCor=0; @Override public boolean onTouchEvent(MotionEvent event) { //System.out.println("flag:"+flag); if(flag == 0) //When manual touch was done { //Set flag to avoid the looping flag=1; //Get the manual touch co-ordinates for Auto Touch next time xCor=event.getRawX(); yCor=event.getRawY(); TestThread(); } System.out.println("onTouchEvent,"+"X:"+event.getRawX()+", Y:"+event.getRawY()); return true; //return false; } /* @Override public boolean onInterceptTouchEvent(MotionEvent event) { System.out.println("intercepted..."); System.out.println("onTouchEvent,"+"X:"+event.getRawX()+", Y:"+event.getRawY()); if(flag == 0) //When manual touch was done { //Set flag to avoid the looping flag=1; //Get the manual touch co-ordinates for Auto Touch next time xCor=event.getRawX(); yCor=event.getRawY(); TestThread(); } onTouchEvent(event); return false; }*/ } 

Please point me in the right direction.

+5
source share

Source: https://habr.com/ru/post/1211935/


All Articles