本文整理了Java中android.app.Instrumentation.getTargetContext()
方法的一些代码示例,展示了Instrumentation.getTargetContext()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Instrumentation.getTargetContext()
方法的具体详情如下:
包路径:android.app.Instrumentation
类名称:Instrumentation
方法名:getTargetContext
暂无
代码示例来源:origin: android-hacker/VirtualXposed
@Override
public Context getTargetContext() {
return base.getTargetContext();
}
代码示例来源:origin: RobotiumTech/robotium
/**
* Sets if mobile data should be turned on or off. Requires android.permission.CHANGE_NETWORK_STATE in the AndroidManifest.xml of the application under test.
*
* @param turnedOn true if mobile data is to be turned on and false if not
*/
public void setMobileData(Boolean turnedOn){
ConnectivityManager dataManager=(ConnectivityManager)instrumentation.getTargetContext().getSystemService(Context.CONNECTIVITY_SERVICE);
Method dataClass = null;
try {
dataClass = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataClass.setAccessible(true);
dataClass.invoke(dataManager, turnedOn);
} catch (Exception e) {
e.printStackTrace();
}
}
代码示例来源:origin: RobotiumTech/robotium
/**
* Sets if wifi data should be turned on or off. Requires android.permission.CHANGE_WIFI_STATE in the AndroidManifest.xml of the application under test.
*
*
* @param turnedOn true if mobile wifi is to be turned on and false if not
*/
public void setWiFiData(Boolean turnedOn){
WifiManager wifiManager = (WifiManager)instrumentation.getTargetContext().getSystemService(Context.WIFI_SERVICE);
try{
wifiManager.setWifiEnabled(turnedOn);
}catch(Exception e){
e.printStackTrace();
}
}
}
代码示例来源:origin: RobotiumTech/robotium
/**
* Returns a localized string
*
* @param id the resource ID for the string
* @return the localized string
*/
public String getString(String id)
{
Context targetContext = instrumentation.getTargetContext();
String packageName = targetContext.getPackageName();
int viewId = targetContext.getResources().getIdentifier(id, "string", packageName);
if(viewId == 0){
viewId = targetContext.getResources().getIdentifier(id, "string", "android");
}
return getString(viewId);
}
代码示例来源:origin: RobotiumTech/robotium
/**
* Returns the height of the scroll or list view parent
* @param view the view who's parents height should be returned
* @return the height of the scroll or list view parent
*/
@SuppressWarnings("deprecation")
public float getScrollListWindowHeight(View view) {
final int[] xyParent = new int[2];
View parent = getScrollOrListParent(view);
final float windowHeight;
if(parent == null){
WindowManager windowManager = (WindowManager)
instrumentation.getTargetContext().getSystemService(Context.WINDOW_SERVICE);
windowHeight = windowManager.getDefaultDisplay().getHeight();
}
else{
parent.getLocationOnScreen(xyParent);
windowHeight = xyParent[1] + parent.getHeight();
}
parent = null;
return windowHeight;
}
代码示例来源:origin: RobotiumTech/robotium
/**
* Scrolls horizontally.
*
* @param side the side to which to scroll; {@link Side#RIGHT} or {@link Side#LEFT}
* @param scrollPosition the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.55.
* @param stepCount how many move steps to include in the scroll. Less steps results in a faster scroll
*/
@SuppressWarnings("deprecation")
public void scrollToSide(Side side, float scrollPosition, int stepCount) {
WindowManager windowManager = (WindowManager)
inst.getTargetContext().getSystemService(Context.WINDOW_SERVICE);
int screenHeight = windowManager.getDefaultDisplay()
.getHeight();
int screenWidth = windowManager.getDefaultDisplay()
.getWidth();
float x = screenWidth * scrollPosition;
float y = screenHeight / 2.0f;
if (side == Side.LEFT)
drag(70, x, y, y, stepCount);
else if (side == Side.RIGHT)
drag(x, 0, y, y, stepCount);
}
代码示例来源:origin: robolectric/robolectric
@Override
public void requestPermissions() {
checkNotNull(permissions);
Application application =
(Application) InstrumentationRegistry.getInstrumentation().getTargetContext();
ShadowApplication shadowApplication = Shadow.extract(application);
shadowApplication.grantPermissions(permissions);
}
}
代码示例来源:origin: RobotiumTech/robotium
/**
* Returns a {@code View} with a given id.
*
* @param id the id of the {@link View} to return
* @param index the index of the {@link View}. {@code 0} if only one is available
* @return a {@code View} with a given id
*/
public View getView(String id, int index){
View viewToReturn = null;
Context targetContext = instrumentation.getTargetContext();
String packageName = targetContext.getPackageName();
int viewId = targetContext.getResources().getIdentifier(id, "id", packageName);
if(viewId != 0){
viewToReturn = getView(viewId, index, TIMEOUT);
}
if(viewToReturn == null){
int androidViewId = targetContext.getResources().getIdentifier(id, "id", "android");
if(androidViewId != 0){
viewToReturn = getView(androidViewId, index, TIMEOUT);
}
}
if(viewToReturn != null){
return viewToReturn;
}
return getView(viewId, index);
}
代码示例来源:origin: Bilibili/DanmakuFlameMaster
public Context getContext(){
return getInstrumentation().getTargetContext();
}
代码示例来源:origin: Karumi/Rosie
private MainApplication getApplication() {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
MainApplication app =
(MainApplication) instrumentation.getTargetContext().getApplicationContext();
return app;
}
代码示例来源:origin: jdamcd/android-crop
@Override
public void setUp() throws Exception {
super.setUp();
// Work around dexmaker issue when running tests on Android 4.3
System.setProperty("dexmaker.dexcache",
getInstrumentation().getTargetContext().getCacheDir().getPath());
}
代码示例来源:origin: ankidroid/Anki-Android
protected Context getTargetContext() {
return InstrumentationRegistry.getInstrumentation().getTargetContext();
}
代码示例来源:origin: android10/Android-CleanArchitecture
private Intent createTargetIntent() {
Intent intentLaunchActivity =
UserDetailsActivity.getCallingIntent(getInstrumentation().getTargetContext(), FAKE_USER_ID);
return intentLaunchActivity;
}
}
代码示例来源:origin: android10/Android-CleanArchitecture
private Intent createTargetIntent() {
Intent intentLaunchActivity =
UserListActivity.getCallingIntent(getInstrumentation().getTargetContext());
return intentLaunchActivity;
}
}
代码示例来源:origin: avjinder/Minimal-Todo
private StoreRetrieveData getDataStorage() {
Context context = getInstrumentation().getTargetContext();
return new StoreRetrieveData(context, MainActivity.FILENAME);
}
}
代码示例来源:origin: square/leakcanary
public @NonNull InstrumentationLeakResults detectLeaks() {
Instrumentation instrumentation = getInstrumentation();
Context context = instrumentation.getTargetContext();
RefWatcher refWatcher = LeakCanary.installedRefWatcher();
Set<String> retainedKeys = refWatcher.getRetainedKeys();
代码示例来源:origin: ankidroid/Anki-Android
protected Collection getCol() {
return CollectionHelper.getInstance().getCol(InstrumentationRegistry.getInstrumentation().getTargetContext());
}
代码示例来源:origin: stephentuso/welcome-android
@Before
public void initActivity() {
instrumentation = InstrumentationRegistry.getInstrumentation();
Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(TestActivity.class.getName(), null, false);
instrumentation.addMonitor(monitor);
Intent intent = new Intent(instrumentation.getTargetContext(), TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
instrumentation.startActivitySync(intent);
activity = instrumentation.waitForMonitor(monitor);
assertNotNull(activity);
}
代码示例来源:origin: facebook/facebook-android-sdk
protected void setUp() throws Exception {
super.setUp();
// Make sure the logging is turned on.
FacebookSdk.setIsDebugEnabled(true);
// Make sure we have read application ID and secret.
readApplicationIdAndSecret();
FacebookSdk.sdkInitialize(getInstrumentation().getTargetContext());
FacebookSdk.setApplicationId(applicationId);
FacebookSdk.setClientToken(clientToken);
// These are useful for debugging unit test failures.
FacebookSdk.addLoggingBehavior(LoggingBehavior.REQUESTS);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
// We want the UI thread to be in StrictMode to catch any violations.
// TODO: reenable this
// turnOnStrictModeForUiThread();
// Needed to bypass a dexmaker bug for mockito
System.setProperty("dexmaker.dexcache",
getInstrumentation().getTargetContext().getCacheDir().getPath());
}
代码示例来源:origin: Karumi/Rosie
@Before public void setUp() {
MainApplication application = getApplication();
List<Object> childTestModules = getTestModules();
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
List<Object> testModules = new LinkedList<>(childTestModules);
testModules.add(new BaseTestModule(context));
ObjectGraph objectGraph = application.plusGraph(testModules);
application.replaceGraph(objectGraph);
objectGraph.inject(this);
}
内容来源于网络,如有侵权,请联系作者删除!