我可以从我的代码启动应用程序,但无法将触摸模拟发送到该应用程序中的按钮。这是主要代码。编译时没有错误。我曾经使用过dispatchevent但是因为那个没有用,我把它删除了
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);
}
}
答案 0 :(得分:0)
您可以使用root方法:
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是坐标。