2017-05-21 7 views
0

自分のコードからアプリを起動できますが、そのアプリのボタンにタッチシミュレーションを送信できません。ここにメインコードがあります。コンパイル時にエラーはありません。私はは、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

あなたは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は座標です。

関連する問題