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

启动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();
 }
       
}

相关文章:

  • 2022-12-23
  • 2021-08-10
  • 2022-12-23
  • 2021-11-01
  • 2021-08-06
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-13
  • 2021-12-20
  • 2021-06-24
  • 2022-12-23
  • 2022-01-26
  • 2021-11-22
  • 2022-12-23
相关资源
相似解决方案