I'm programming for a virtual mouse cursor by Android studio and that is from an example in this website and its code. I have an overlay cursor and following code to go back to the home screen.
Intent newActivity = new Intent(Intent.ACTION_MAIN);
newActivity.addCategory(Intent.CATEGORY_HOME);
newActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newActivity);
My problem is how can I apply the click action to my cursor when the view is outside my app (maybe home screen or other apps). I tried this
Instrumentation m_Instrumentation = new Instrumentation();
m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,new_x, new_y, 0));
m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, new_x, new_y, 0));
but it shown
Injecting to another application requires INJECT_EVENTS permission.
Then, I added
<uses-permission android:name="android.permission.INJECT_EVENTS"/>
in AndroidManifest.xml. There is an error called "Permission is only granted to system apps" so that I can't use it in my app to execute virtual click. If I just close the inspection system, it still doesn't work to my app.
After that, I tried dispatchTouchEvent with Handler but it only works in myself app (Can't be used in the home screen to open other apps.)
dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis() + 100, MotionEvent.ACTION_DOWN, new_x, new_y, 0));
dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis() + 100, MotionEvent.ACTION_UP, new_x, new_y, 0));
Thanks for your replying.
My simulated mobile device is Android 4.4.2.
Compile Sdk version: Android 6.0
Build tool version: 24.0.3