本文整理了Java中android.app.Application.getBaseContext()
方法的一些代码示例,展示了Application.getBaseContext()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Application.getBaseContext()
方法的具体详情如下:
包路径:android.app.Application
类名称:Application
方法名:getBaseContext
暂无
代码示例来源:origin: Tencent/tinker
@Override
public Context getBaseContext() {
Context base = super.getBaseContext();
if (applicationLike != null) {
return (Context) invokeAppLikeGetBaseContext(applicationLike, base);
}
return base;
}
代码示例来源:origin: android-hacker/VirtualXposed
private void fixWeChatRecovery(Application app) {
try {
Field field = app.getClassLoader().loadClass("com.tencent.recovery.Recovery").getField("context");
field.setAccessible(true);
if (field.get(null) != null) {
return;
}
field.set(null, app.getBaseContext());
} catch (Throwable e) {
e.printStackTrace();
}
}
代码示例来源:origin: oasisfeng/condom
@Override public Context getBaseContext() {
mCondom.logConcern(TAG, "Application.getBaseContext");
return super.getBaseContext();
}
代码示例来源:origin: robolectric/robolectric
/**
* @deprecated Do not depend on this method to override services as it will be removed in a future
* update. The preferered method is use the shadow of the corresponding service.
*/
@Deprecated
public void setSystemService(String key, Object service) {
ShadowContextImpl shadowContext = Shadow.extract(realApplication.getBaseContext());
shadowContext.setSystemService(key, service);
}
代码示例来源:origin: robolectric/robolectric
private BackupAgentController<T> attach() {
if (attached) {
return this;
}
Context baseContext = RuntimeEnvironment.application.getBaseContext();
ReflectionHelpers.callInstanceMethod(BackupAgent.class, component, "attach",
ReflectionHelpers.ClassParameter.from(Context.class, baseContext));
return this;
}
代码示例来源:origin: robolectric/robolectric
/**
* Create and register {@link ContentProvider} using {@link ProviderInfo} found from manifest.
*/
public ContentProviderController<T> create() {
Context baseContext = RuntimeEnvironment.application.getBaseContext();
ComponentName componentName = createRelative(baseContext.getPackageName(), contentProvider.getClass().getName());
ProviderInfo providerInfo = null;
try {
providerInfo =
baseContext
.getPackageManager()
.getProviderInfo(componentName, PackageManager.MATCH_DISABLED_COMPONENTS);
} catch (PackageManager.NameNotFoundException e) {
Logger.strict("Unable to find provider info for " + componentName, e);
}
return create(providerInfo);
}
代码示例来源:origin: robolectric/robolectric
private ActivityController<T> attach(
@Nullable @WithType("android.app.Activity$NonConfigurationInstances")
Object lastNonConfigurationInstances) {
if (attached) {
return this;
}
// make sure the component is enabled
Context context = RuntimeEnvironment.application.getBaseContext();
context
.getPackageManager()
.setComponentEnabledSetting(
new ComponentName(context.getPackageName(), component.getClass().getName()),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
0);
ShadowActivity shadowActivity = Shadow.extract(component);
shadowActivity.callAttach(getIntent(), lastNonConfigurationInstances);
attached = true;
return this;
}
代码示例来源:origin: robolectric/robolectric
/**
* Create and register {@link ContentProvider} using the given {@link ProviderInfo}.
*
* @param providerInfo the {@link ProviderInfo} to use
* @return this {@link ContentProviderController}
*/
public ContentProviderController<T> create(ProviderInfo providerInfo) {
Context baseContext = RuntimeEnvironment.application.getBaseContext();
// make sure the component is enabled
ComponentName componentName =
createRelative(baseContext.getPackageName(), contentProvider.getClass().getName());
baseContext
.getPackageManager()
.setComponentEnabledSetting(
componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
contentProvider.attachInfo(baseContext, providerInfo);
if (providerInfo != null) {
ShadowContentResolver.registerProviderInternal(providerInfo.authority, contentProvider);
}
return this;
}
代码示例来源:origin: robolectric/robolectric
private ServiceController<T> attach() {
if (attached) {
return this;
}
// make sure the component is enabled
Context context = RuntimeEnvironment.application.getBaseContext();
ComponentName name =
new ComponentName(context.getPackageName(), component.getClass().getName());
context
.getPackageManager()
.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
ReflectionHelpers.callInstanceMethod(Service.class, component, "attach",
from(Context.class, RuntimeEnvironment.application.getBaseContext()),
from(ActivityThread.class, null),
from(String.class, component.getClass().getSimpleName()),
from(IBinder.class, null),
from(Application.class, RuntimeEnvironment.application),
from(Object.class, null));
attached = true;
return this;
}
代码示例来源:origin: robolectric/robolectric
private IntentServiceController<T> attach() {
if (attached) {
return this;
}
// make sure the component is enabled
Context context = RuntimeEnvironment.application.getBaseContext();
ComponentName name =
new ComponentName(context.getPackageName(), component.getClass().getName());
context
.getPackageManager()
.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
ReflectionHelpers.callInstanceMethod(Service.class, component, "attach",
from(Context.class, RuntimeEnvironment.application.getBaseContext()),
from(ActivityThread.class, null),
from(String.class, component.getClass().getSimpleName()),
from(IBinder.class, null),
from(Application.class, RuntimeEnvironment.application),
from(Object.class, null));
attached = true;
return this;
}
代码示例来源:origin: oasisfeng/condom
@Test public void testApplicationContextAsBaseContext() throws Exception {
final Context context = InstrumentationRegistry.getTargetContext();
final Context app_context = context.getApplicationContext();
assertTrue(app_context instanceof Application);
final CondomContext condom_context = CondomContext.wrap(app_context, TAG);
final Context condom_app_context = condom_context.getApplicationContext();
assertTrue(condom_app_context instanceof Application);
assertEquals(condom_context, ((Application) condom_app_context).getBaseContext());
assertTrue(((Application) condom_app_context).getBaseContext() instanceof CondomContext);
}
代码示例来源:origin: oasisfeng/condom
@Test public void testApplicationAsApplicationContextOfBaseContext() throws Exception {
final Context context = InstrumentationRegistry.getTargetContext();
final Context app_context = context.getApplicationContext();
assertTrue(app_context instanceof Application);
final CondomContext condom_context = CondomContext.wrap(context, TAG);
final Context condom_app_context = condom_context.getApplicationContext();
assertTrue(condom_app_context instanceof Application);
assertNotSame(app_context, condom_app_context);
assertTrue(((Application) condom_app_context).getBaseContext() instanceof CondomContext);
}
代码示例来源:origin: robolectric/robolectric
Object lastNonConfigurationInstances) {
Application application = RuntimeEnvironment.application;
Context baseContext = application.getBaseContext();
代码示例来源:origin: robolectric/robolectric
@Test
public void testSet24HourMode_12() {
ShadowSettings.set24HourTimeFormat(false);
assertThat(DateFormat.is24HourFormat(context.getBaseContext())).isFalse();
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testSet24HourMode_24() {
ShadowSettings.set24HourTimeFormat(true);
assertThat(DateFormat.is24HourFormat(context.getBaseContext())).isTrue();
}
代码示例来源:origin: android-hacker/VirtualXposed
private void handleReceiver(ReceiverData data) {
BroadcastReceiver.PendingResult result = data.resultData.build();
try {
if (!isBound()) {
bindApplication(data.component.getPackageName(), data.processName);
}
Context context = mInitialApplication.getBaseContext();
Context receiverContext = ContextImpl.getReceiverRestrictedContext.call(context);
String className = data.component.getClassName();
BroadcastReceiver receiver = (BroadcastReceiver) context.getClassLoader().loadClass(className).newInstance();
mirror.android.content.BroadcastReceiver.setPendingResult.call(receiver, result);
data.intent.setExtrasClassLoader(context.getClassLoader());
if (data.intent.getComponent() == null) {
data.intent.setComponent(data.component);
}
receiver.onReceive(receiverContext, data.intent);
if (mirror.android.content.BroadcastReceiver.getPendingResult.call(receiver) != null) {
result.finish();
}
} catch (Exception e) {
// must be this for misjudge of anti-virus!!
throw new RuntimeException(String.format("Unable to start receiver: %s ", data.component), e);
}
VActivityManager.get().broadcastFinish(data.resultData);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void getRunningAppProcesses_shouldReturnProcessList() {
final ActivityManager activityManager = getActivityManager();
final ActivityManager.RunningAppProcessInfo process1 = buildProcessInfo(new ComponentName("org.robolectric", "Process 1"));
final ActivityManager.RunningAppProcessInfo process2 = buildProcessInfo(new ComponentName("org.robolectric", "Process 2"));
assertThat(activityManager.getRunningAppProcesses().size()).isEqualTo(1);
ActivityManager.RunningAppProcessInfo myInfo = activityManager.getRunningAppProcesses().get(0);
assertThat(myInfo.pid).isEqualTo(android.os.Process.myPid());
assertThat(myInfo.uid).isEqualTo(android.os.Process.myUid());
assertThat(myInfo.processName)
.isEqualTo(
((Application) ApplicationProvider.getApplicationContext())
.getBaseContext()
.getPackageName());
shadowOf(activityManager).setProcesses(Lists.newArrayList(process1, process2));
assertThat(activityManager.getRunningAppProcesses()).containsExactly(process1, process2);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void shouldSetBaseContext() throws Exception {
MyBackupAgent myBackupAgent = backupAgentController.get();
assertThat(myBackupAgent.getBaseContext())
.isEqualTo(((Application) ApplicationProvider.getApplicationContext()).getBaseContext());
}
代码示例来源:origin: robolectric/robolectric
@Test
public void shouldSetBaseContext() throws Exception {
TestContentProvider1 myContentProvider = controller.create().get();
assertThat(myContentProvider.getContext())
.isEqualTo(((Application) ApplicationProvider.getApplicationContext()).getBaseContext());
}
代码示例来源:origin: robolectric/robolectric
@Test
public void soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler() throws Exception {
Looper mainLooper = Looper.getMainLooper();
Scheduler scheduler = shadowOf(mainLooper).getScheduler();
shadowOf(mainLooper).quit = true;
assertThat(ApplicationProvider.getApplicationContext().getMainLooper()).isSameAs(mainLooper);
Scheduler s = new Scheduler();
RuntimeEnvironment.setMasterScheduler(s);
ShadowLooper.resetThreadLoopers();
Application application = new Application();
ReflectionHelpers.callInstanceMethod(
application,
"attach",
ReflectionHelpers.ClassParameter.from(
Context.class,
((Application) ApplicationProvider.getApplicationContext()).getBaseContext()));
assertThat(Looper.getMainLooper()).named("Looper.getMainLooper()").isSameAs(mainLooper);
assertThat(application.getMainLooper()).named("app.getMainLooper()").isSameAs(mainLooper);
assertThat(shadowOf(mainLooper).getScheduler()).named("scheduler").isNotSameAs(scheduler);
assertThat(shadowOf(mainLooper).getScheduler()).named("scheduler").isSameAs(s);
assertThat(shadowOf(mainLooper).hasQuit()).named("quit").isFalse();
}
内容来源于网络,如有侵权,请联系作者删除!