我想做什么? - 基本上我想在特定时间在特定的X,Y坐标上实用地进行人体触摸 - 运行一个服务,通过某个接口(TCP / UDP)接收X,Y坐标 - 使用这些坐标消防dispatchTouchEvent()并获得实际人手指点击给出的结果 - 我的意思是焦点活动可能是Android主屏幕或任何打开的活动或任何东西 - moment服务调用dispatchTouchEvent(),它应该执行单击。 - 例如,如果打开App是计算器,并且dispatchTouchEvent()的X,Y坐标指向数字5,则应按5键
我现在做了什么? - 我已经开始接收X,Y坐标的服务 - 在某些触发器上,它调用dispatchTouchEvent(),但它不起作用。它不会点击屏幕上的任何内容。 - 它捕捉人类手指所做的每一次触摸事件。
问题是什么? - 如何将dispatchTouchEvent()发布到任何底层活动
示例代码: 我在这里附上示例代码。我刚刚将它简化为测试目的。 首先,我们需要点击屏幕中的任意位置。将保存其X,Y坐标,并在一段时间后调用dispatchTouchEvent() 使用相同的坐标。
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;
}*/
}
请指出正确的方向。