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));
     
     
  • 相关阅读:
    android注解使用具体解释(图文)
    Zepto Code Rush 2014-A. Feed with Candy(HACK)
    LoadRunner解决超时错误
    Loadrunner定时执行脚本
    windows下at命令使用详解
    java编写Loadrunner脚本
    Mysql 查看连接数,状态
    获取ping的最短、最长、平均时间
    mysql中You can't specify target table for update in FROM clause错误
    修改Apache的最大连接数
  • 原文地址:https://www.cnblogs.com/sode/p/2961389.html
Copyright © 2011-2022 走看看