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);
|