Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android onTouchEvent通过检测通过代码实现_Android_Android Instrumentation - Fatal编程技术网

Android onTouchEvent通过检测通过代码实现

Android onTouchEvent通过检测通过代码实现,android,android-instrumentation,Android,Android Instrumentation,我想做到以下几点: I have two buttons. When first button is pressed, it will fire onTouchEvent on second button, thus pressing second button. 以下是触发事件的代码摘录: int test1[] = new int[2]; button2.getLocationInWindow(test1); //--->getting coordinates of second

我想做到以下几点:

I have two buttons. When first button is pressed,
it will fire onTouchEvent on second button, thus pressing second button.
以下是触发事件的代码摘录:

int test1[] = new int[2];
button2.getLocationInWindow(test1);   //--->getting coordinates of second button
Instrumentation m_Instrumentation = new Instrumentation();
//firing event
m_Instrumentation.sendPointerSync(MotionEvent.obtain(android.os.SystemClock.uptimeMillis(),android.os.SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,test1[0]+10, test1[1]+10, 0));
注意:我使用genymotion emulator

错误日志

07-08 12:47:38.743: E/InputEventReceiver(6849): Exception dispatching input event.
07-08 12:47:38.743: E/MessageQueue-JNI(6849): Exception in MessageQueue callback: handleReceiveCallback
07-08 12:47:38.743: E/MessageQueue-JNI(6849): java.lang.RuntimeException: This method can not be called from the main application thread
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.app.Instrumentation.validateNotAppThread(Instrumentation.java:1651)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.app.Instrumentation.sendPointerSync(Instrumentation.java:933)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at com.example.touchtest1.MainActivity.fireEvent(MainActivity.java:55)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at com.example.touchtest1.MainActivity$MyTouchListener.onTouch(MainActivity.java:75)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.View.dispatchTouchEvent(View.java:7701)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:260)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.View.dispatchPointerEvent(View.java:7886)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at  android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
07-08 12:47:38.743: E/MessageQueue-JNI(6849):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
然而,这并没有达到预期的效果。问题出在哪里

对于基于错误和的

,看起来您无法在主线程(换句话说,UI线程)上运行此操作

一个可能的解决方案是在另一个线程中运行它。它看起来是这样的:

int test1[] = new int[2];
button2.getLocationInWindow(test1);   //--->getting coordinates of second button
final Instrumentation m_Instrumentation = new Instrumentation();
//firing event

new Thread(new Runnable() {
    @Override
    public void run() {
        m_Instrumentation.sendPointerSync(MotionEvent.obtain(
               android.os.SystemClock.uptimeMillis(),
               android.os.SystemClock.uptimeMillis(),
               MotionEvent.ACTION_DOWN,test1[0]+10, test1[1]+10, 0));
    }
}).start();

请注意,我已将
m_Instrumentation
更改为
final
变量,以便它可以在不同的线程中使用。

Instrumentation无法在主(UI)线程上工作

您可以尝试实现一个AsyncTask类,它可以在后台为您完成工作。实现之后,您只需要从类中的方法调用它

 private void CREATE_EVENT()
 {
    long action_time = SystemClock.uptimeMillis();

    //MotionEvent.obtain(long downTime, long eventTime, int action, float x, float y, int metaState);
    MotionEvent motion_event = MotionEvent.obtain(action_time, action_time, MotionEvent.ACTION_DOWN, 250, 150, 0); 

    new INJECT_EVENTS().execute(motion_event);
  }

 public class INJECT_EVENTS extends AsyncTask<MotionEvent, MotionEvent, MotionEvent>
 {
    @Override
    protected MotionEvent doInBackground(MotionEvent ... event) {

        Instrumentation inject_event = new Instrumentation();
        event[0].recycle();
        inject_event.sendPointerSync(event[0]);
        Log.i("TEST", event[0].toString() + " __________________________EVENT CREATED!");
        return null;
    }
 }
private void CREATE_事件()
{
长动作时间=SystemClock.uptimeMillis();
//获取(长停机时间、长事件时间、int操作、浮点x、浮点y、int元状态);
MotionEvent motion\u event=MotionEvent.get(action\u time,action\u time,MotionEvent.action\u DOWN,250,150,0);
新注入事件().execute(运动事件);
}
公共类注入事件扩展异步任务
{
@凌驾
受保护的MotionEvent doInBackground(MotionEvent…事件){
插装注入事件=新插装();
事件[0]。回收();
inject_event.sendPointerSync(事件[0]);
Log.i(“TEST”,事件[0]。toString()+“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu事件已创建!”);
返回null;
}
}

什么是“不按计划进行”呢?当您尝试此操作时,实际发生了什么?@AndrewSchuster,它会给出错误。现在,我将用错误更新log@AndrewSchuster,我更新了错误日志