Hi,
I'm in need of a context object from inside framework code. The
framework code in question doesn't have access to a context because it
is by definition not loaded explicitly by an app, but rather is listed
in frameworks/base/preloaded-classes. Because apps never call it
directly, there is no obvious opportunity to pass a Context to it (it
is called indirectly as it is registered to be called from some
java.net classes).
This code seems to work, but I wonder if it's the Right Thing to do
and if there are consequences I'm not aware of (a similar snippet
appears in the GeolocationService ctor).
ActivityThread activityThread =
ActivityThread.currentActivityThread();
if (activityThread == null) {
Looper.prepare(); // in case this hasn't been called yet -
otherwise systemMain() fails
activityThread = ActivityThread.systemMain();
}
applicationContext = activityThread.getApplication();
Any thoughts?
Thanks,
- Guy.