zoukankan      html  css  js  c++  java
  • android 模拟点击2

    引用:http://zhidao.baidu.com/question/127090389.html

    启动activity   可以使用InstrumentationTestCase
    发送手机事件
    Instrumentation.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN);
    Instrumentation.sendCharacterSync(KeyEvent.KEYCODE_DPAD_CENTER);

    使用android的测试功能,写一个类继承InstrumentationTestCase
    然后在这个类里获得Instrumentation实例,通过它可以启动Activity,发送手机事件等

    ------------------------------------------------------
    import android.app.Instrumentation;
    import android.content.ContentResolver;

    public class ActivityTest extends InstrumentationTestCase {

    private Instrumentation mInst = null;
    private ContentResolver mContentResolver = null;

    @Override
    protected void setUp() throws Exception {
    super.setUp();
    mInst = getInstrumentation();
    mContentResolver = mInst.getContext().getContentResolver();


    }

    public void testStartActivity() throws Exception {
    //launch activity
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    String activityPackagePath = "com.android.";
    intent.setClassName(activityPackagePath, TargetActivity.getClass().getName());
    TargetActivity mActivity = (TargetActivity) getInstrumentation().startActivitySync(intent);
    mInst.waitForIdleSync();
    //send keyevent to press button
    mInst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN);
    mInst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_CENTER);
    mInst.waitForIdleSync();
    }

    }

    ===================================
    View.performClick();

    MotionEvent me=MotionEvent.obtain(0,0,MotionEvent.ACTION_DOWN,x,y,0);
    onTouch(mPageWidget, me);

    可用Instrumentation 模拟触摸时间
    例如触摸点(240,400):
    Instrumentation inst=new Instrumentation();
    inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 240, 400, 0));
    inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 240, 400, 0));
     
     
  • 相关阅读:
    axios拦截器
    Vue路由守卫
    HTML横向滚动条和文本超出显示三个小圆点
    Vue用户名vuex和localStorage双向存储
    javaScript Es6数组与对象的实例方法
    使用vue实现复选框单选多选
    正则表达式常用字符
    jest函数单元测试
    ts中的类
    ts中接口的用法
  • 原文地址:https://www.cnblogs.com/sode/p/2961389.html
Copyright © 2011-2022 走看看