本文整理匯總了Java中android.app.Instrumentation.getContext方法的典型用法代碼示例。如果您正苦於以下問題:Java Instrumentation.getContext方法的具體用法?Java Instrumentation.getContext怎麽用?Java Instrumentation.getContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.app.Instrumentation
的用法示例。
在下文中一共展示了Instrumentation.getContext方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testA
import android.app.Instrumentation; //導入方法依賴的package包/類
/**
* 測試CollapsingToolbarLayout
* 被測Demo下載地址:https://github.com/alidili/DesignSupportDemo
*
* @throws UiObjectNotFoundException
*/
public void testA() throws UiObjectNotFoundException {
// 獲取設備對象
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
UiDevice uiDevice = UiDevice.getInstance(instrumentation);
// 獲取上下文
Context context = instrumentation.getContext();
// 啟動測試App
Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.yang.designsupportdemo");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
// 打開CollapsingToolbarLayout
String resourceId = "com.yang.designsupportdemo:id/CollapsingToolbarLayout";
UiObject collapsingToolbarLayout = uiDevice.findObject(new UiSelector().resourceId(resourceId));
collapsingToolbarLayout.click();
for (int i = 0; i < 5; i++) {
// 向上移動
uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(),
uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2, 10);
// 向下移動
uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2,
uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(), 10);
}
// 點擊應用返回按鈕
UiObject back = uiDevice.findObject(new UiSelector().description("Navigate up"));
back.click();
// 點擊設備返回按鈕
uiDevice.pressBack();
}
示例2: testB
import android.app.Instrumentation; //導入方法依賴的package包/類
/**
* 滑動界麵,打開About phone選項
* 測試環境為標準Android 7.1.1版本,不同設備控件查找方式會有不同
*
* @throws UiObjectNotFoundException
*/
public void testB() throws UiObjectNotFoundException {
// 獲取設備對象
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
UiDevice uiDevice = UiDevice.getInstance(instrumentation);
// 獲取上下文
Context context = instrumentation.getContext();
// 點擊Settings按鈕
UiObject uiObject = uiDevice.findObject(new UiSelector().description("Settings"));
uiObject.click();
// 滑動列表到最後,點擊About phone選項
UiScrollable settings = new UiScrollable(new UiSelector().className("android.support.v7.widget.RecyclerView"));
UiObject about = settings.getChildByText(new UiSelector().className("android.widget.LinearLayout"), "About phone");
about.click();
// 點擊設備返回按鈕
uiDevice.pressBack();
uiDevice.pressBack();
}
示例3: getWriteableDir
import android.app.Instrumentation; //導入方法依賴的package包/類
/**
* Prefer internal over external storage, because external tends to be FAT filesystems,
* which don't support symlinks (which we test using this method).
*/
public static File getWriteableDir(Instrumentation instrumentation) {
Context context = instrumentation.getContext();
Context targetContext = instrumentation.getTargetContext();
File[] dirsToTry = new File[]{
context.getCacheDir(),
context.getFilesDir(),
targetContext.getCacheDir(),
targetContext.getFilesDir(),
context.getExternalCacheDir(),
context.getExternalFilesDir(null),
targetContext.getExternalCacheDir(),
targetContext.getExternalFilesDir(null),
Environment.getExternalStorageDirectory(),
};
return getWriteableDir(dirsToTry);
}