下面列出了android.app.Instrumentation#startActivitySync ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private long launchCamera()
{
long startupTime = 0;
try
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(getInstrumentation().getTargetContext(), CameraActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
long beforeStart = System.currentTimeMillis();
Instrumentation inst = getInstrumentation();
Activity cameraActivity = inst.startActivitySync(intent);
long cameraStarted = System.currentTimeMillis();
Thread.sleep(WAIT_TIME_FOR_PREVIEW);
cameraActivity.finish();
startupTime = cameraStarted - beforeStart;
Thread.sleep(1000);
Log.v(TAG, "camera startup time: " + startupTime);
} catch (Exception e)
{
Log.v(TAG, "Got exception", e);
fail("Fails to get the output file");
}
return startupTime;
}
private long launchVideo()
{
long startupTime = 0;
try
{
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
intent.setClass(getInstrumentation().getTargetContext(), CameraActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
long beforeStart = System.currentTimeMillis();
Instrumentation inst = getInstrumentation();
Activity recorderActivity = inst.startActivitySync(intent);
long cameraStarted = System.currentTimeMillis();
recorderActivity.finish();
startupTime = cameraStarted - beforeStart;
Log.v(TAG, "Video Startup Time = " + startupTime);
// wait for 1s to make sure it reach a clean stage
Thread.sleep(WAIT_TIME_FOR_PREVIEW);
Log.v(TAG, "video startup time: " + startupTime);
} catch (Exception e)
{
Log.v(TAG, "Got exception", e);
fail("Fails to launch video output file");
}
return startupTime;
}
public void testBackImageCapture() throws Exception
{
Instrumentation inst = getInstrumentation();
Intent intent = new Intent();
intent.setClass(getInstrumentation().getTargetContext(), CameraActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(EXTRAS_CAMERA_FACING,
android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK);
Activity act = inst.startActivitySync(intent);
Thread.sleep(WAIT_FOR_SWITCH_CAMERA);
captureImages("Back Camera Image Capture\n", inst);
act.finish();
}
public void testFrontImageCapture() throws Exception
{
Instrumentation inst = getInstrumentation();
Intent intent = new Intent();
intent.setClass(getInstrumentation().getTargetContext(), CameraActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(EXTRAS_CAMERA_FACING,
android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
Activity act = inst.startActivitySync(intent);
Thread.sleep(WAIT_FOR_SWITCH_CAMERA);
captureImages("Front Camera Image Capture\n", inst);
act.finish();
}
public void testBackVideoCapture() throws Exception
{
Instrumentation inst = getInstrumentation();
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
intent.setClass(getInstrumentation().getTargetContext(), CameraActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(EXTRAS_CAMERA_FACING,
android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK);
Activity act = inst.startActivitySync(intent);
Thread.sleep(WAIT_FOR_SWITCH_CAMERA);
captureVideos("Back Camera Video Capture\n", inst);
act.finish();
}
public void testFrontVideoCapture() throws Exception
{
Instrumentation inst = getInstrumentation();
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
intent.setClass(getInstrumentation().getTargetContext(), CameraActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(EXTRAS_CAMERA_FACING,
android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
Activity act = inst.startActivitySync(intent);
Thread.sleep(WAIT_FOR_SWITCH_CAMERA);
captureVideos("Front Camera Video Capture\n", inst);
act.finish();
}
private void startActivity() {
if (launched)
return;
Instrumentation instrumentation = getInstrumentation();
Intent intent = new Intent(getInstrumentation()
.getTargetContext(), AndroidNativeActivity.class);
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mActivity = instrumentation.startActivitySync(intent);
launched = true;
instrumentation.waitForIdleSync();
sleep(5000);
}
@SuppressWarnings("unchecked") // Guarded by generics at the constructor.
public void launchActivity() {
if(activity != null) {
return;
}
Instrumentation instrumentation = fetchInstrumentation();
String targetPackage = instrumentation.getTargetContext().getPackageName();
Intent intent = getLaunchIntent(targetPackage, activityClass);
activity = (T) instrumentation.startActivitySync(intent);
instrumentation.waitForIdleSync();
}
public AppLaunchInteractor launchApp() {
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
final Intent launchIntent = instrumentation.getTargetContext().getPackageManager()
.getLaunchIntentForPackage(instrumentation.getTargetContext().getPackageName())
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
instrumentation.startActivitySync(launchIntent);
instrumentation.waitForIdleSync();
return new AppLaunchInteractor();
}
@SuppressWarnings("unchecked") // Guarded by generics at the constructor.
private void launchActivity() {
if (activity != null) return;
Instrumentation instrumentation = fetchInstrumentation();
String targetPackage = instrumentation.getTargetContext().getPackageName();
Intent intent = getLaunchIntent(targetPackage, activityClass);
activity = (T) instrumentation.startActivitySync(intent);
instrumentation.waitForIdleSync();
}