如何做到自动安装apk后并自动运行它

1.   安装命令并自动运行apk命令的使用

adb  install –r InstrumentStudy.apk

adb shell aminstrument  –w cn.nubia.instrumentstudy/android.test.InstrumentationTestRunner

2.   代码详解

MainActivity.java

package cn.nubia.instrumentstudy;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button mButton;
	private TextView mTextView;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mButton = (Button) findViewById(R.id.button1);
		mTextView = (TextView) findViewById(R.id.textView1);
		mButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mTextView.setText("变");
			}
		});
	}

	public int sum(int a, int b) {

		return a + b;
	}

	public int substract(int a, int b) {
		return b - a;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


TestMainActivity.java

package cn.nubia.instrummentstudytest;

import cn.nubia.instrumentstudy.MainActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.TextView;

public class TestMainActivity extends
		ActivityInstrumentationTestCase2<MainActivity> {
	private Button mbutton;
	private TextView mtTextView;
	private MainActivity tms;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		setActivityInitialTouchMode(false);
		tms = getActivity();
		mbutton = (Button) tms
				.findViewById(cn.nubia.instrumentstudy.R.id.button1);
		mtTextView = (TextView) tms
				.findViewById(cn.nubia.instrumentstudy.R.id.textView1);
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public TestMainActivity() {

		super(MainActivity.class);
	}

	public void testsum() throws InterruptedException {
		// assertEquals(3, getActivity().sum(1, 2));
		// sendKeys(KeyEvent.KEYCODE_MENU);
		// Thread.sleep(5000);
		sendKeys(KeyEvent.KEYCODE_MENU);
		getActivity().runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Log.i("id", mbutton.toString());
				mbutton.requestFocus();
				mbutton.performClick();
			}
		});
	}

}


AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.nubia.instrumentstudy"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="cn.nubia.instrumentstudy" >
    </instrumentation>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cn.nubia.instrumentstudy.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

3.   注意事项

调试的时候,注意时间控制,可能界面变化太快,导致没有看清测试过程,可通过 SystemClock.sleep(5000);    来控制事件的时间间隔


 

你可能感兴趣的:(如何做到自动安装apk后并自动运行它)