将触摸事件发送到另一个应用程序 [英] Sending touch event to another application

查看:132
本文介绍了将触摸事件发送到另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够从我的代码中启动一个应用程序,但无法将触摸模拟发送到该应用程序中的按钮.这是主要代码.编译时没有错误.我使用了dispatchevent,但由于该方法不起作用,我将其删除了

I am able to launch an app from my code but am not able to send touch simulation to the button in that app.. Here is the main code. There are no errors when compiling. I had used dispatchevent but since that didn't work I removed it

package com.mycompany.myapp3;
import android.app.*;
import android.os.*;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.*;
import android.system.*;
import android.view.MotionEvent;
import android.view.InputDevice.*;
import android.widget.*;
public class MainActivity extends Activity 
{
Button button;
//static public MotionEvent obtain(long downtime,long eventime,int action,float x,float y,int metastate)

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addListenerOnbutton();
    
        simulatepress(-1100,250);

       MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,400,400,0);



    }


public void addListenerOnbutton() {

button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
@Override
    public void onClick(View arg0){
Intent launchIntent=getPackageManager().getLaunchIntentForPackage("com.abc.xyz");
if (launchIntent!=null){
startActivity(launchIntent);
}

pause1(10000);
Toast.makeText(getApplicationContext(),"Waiting over",Toast.LENGTH_SHORT).show();

    simulatepress(-180,25);

}
});


}
@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
// TODO: Implement this method
return super.dispatchTouchEvent(ev);
}
    


public void pause1(long sleeptime){
try{
Thread.sleep(sleeptime);
} catch (InterruptedException ex){

}
}


// our program for generating click
// dont delete


public void simulatepress(long x,long y){
MotionEvent e=MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,x,y,0);
onTouchEvent(e);
//dispatchTouchEvent(e);

}










}

推荐答案

您可以使用根方法:

String x,y = "400"
String[] deviceCommands = {"su", "input touchscreen tap", x, y};
try {
    Process process = Runtime.getRuntime().exec(deviceCommands);
} catch (IOException e) {
   e.printStackTrace();
   Toast.makeText(getApplicationContext(), "error!", Toast.LENGTH_SHORT).show();
}

仅适用于root用户. x和y是坐标.

Only works with root. x and y are the coordinates.

这篇关于将触摸事件发送到另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆