如何将触摸事件发送到android

时间:2017-12-11 04:38:19

标签: android events touch simulate

我正在尝试从我的应用程序向其他应用程序发送触摸事件。

有没有解决方案,没有手机根?

1 个答案:

答案 0 :(得分:1)

helpl to achieve.you可以在MotionEvent中提取基本数据,就像这样。

//援助数据

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);
    }

最后,您应该在服务应用中收到消息。

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

已经完成了。