本文介绍了如何将触摸事件发送到Android中的其他应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从我的应用程序向其他应用程序发送触摸事件.
I'm trying to send touch event to other app from my app.
有没有解决这个问题的解决方案吗?
Is there any solution for that without making phone root?
推荐答案
辅助实现.您可以像这样在MotionEvent中提取基本数据.
aidl to achieve.you can extract the basic data in MotionEvent,like this.
//aidl数据
public class Data implements Parcelable {
private long downTime;
private long eventTime;
private int action;
private float x;
private float y;
private float pressure;
private float size;
private int metaState;
private float xPrecision;
private float yPrecision;
private int deviceId;
private int edgeFlags;
private Data(Parcel source){
readFromParcel(source);
}
public Data(){
}
public static Data.Creator<Data> CREATOR= new Creator<Data>() {
@Override
public Data createFromParcel(Parcel source) {
return new Data(source);
}
@Override
public Data[] newArray(int size) {
return new Data[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(downTime);
dest.writeLong(eventTime);
dest.writeInt(action);
dest.writeFloat(x);
dest.writeFloat(y);
dest.writeFloat(pressure);
dest.writeFloat(size);
dest.writeInt(metaState);
dest.writeFloat(xPrecision);
dest.writeFloat(yPrecision);
dest.writeInt(deviceId);
dest.writeInt(edgeFlags);
}
public void readFromParcel(Parcel source){
edgeFlags=source.readInt();
deviceId=source.readInt();
yPrecision=source.readFloat();
xPrecision=source.readFloat();
metaState=source.readInt();
size=source.readFloat();
pressure=source.readFloat();
y=source.readFloat();
x=source.readFloat();
action=source.readInt();
eventTime=source.readLong();
downTime=source.readLong();
}
}
//aidl
interface IData {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
boolean senMotionEvent(in Data data);
}
发送客户端应用数据:
@Override
public boolean onTouchEvent(final MotionEvent event) {
return super.onTouchEvent(event);
Intent serviceIntent=new Intent();
serviceIntent.setComponent(new ComponentName("XX.XXX.XXX","XX.XXX.XXX.service"));
this.bindService(serviceIntent,new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IData iData= IData.Stub.asInterface(service);
Data data= new Data();
data.downTime=event.getDownTime();
data.eventTime=event.getEventTime();
data.action=event.getAction();
data.size=event.getSize();
data.metaState=event.getMetaState();
data.yPrecision=event.getYPrecision();
data.xPrecision=event.getXPrecision();
data.x=event.getX();
data.y=event.getY();
data.edgeFlags=event.getEdgeFlags();
data.deviceId=event.getDeviceId();
data.pressure=event.getPressure();
try {
boolean isSuccess= iData.senMotionEvent(data);
Log.d("sendEvent","send motionevent result:"+isSuccess);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
LogUtils.debug("serviceConnection is not connected.");
}
},Context.BIND_AUTO_CREATE);
}
最后,您应该在服务应用中收到消息.
finally, you should get message in service app.
public class IDataService extends Service {
public class IDataBinder extends IData.Stub {
private Context mContext;
public IDataBinder(Context context){
this.mContext=context;
}
boolean senMotionEvent(Data data){
//add operation code of services.
return true;
}
/**
* Cast an IBinder object into an Aidl interface,
* generating a proxy if needed.
*/
public static IData asInterface(android.os.IBinder obj) {
return IData.Stub.asInterface(obj);
}
}
IDataBinder iDataBinder=new IDataBinder(this);
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return iDataBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
完成了.
这篇关于如何将触摸事件发送到Android中的其他应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!