Getting the current application context

2,732 views
Skip to first unread message

Guy Rosen

unread,
Sep 28, 2010, 11:51:01 AM9/28/10
to android-platform
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.

Dianne Hackborn

unread,
Sep 28, 2010, 7:35:59 PM9/28/10
to android-...@googlegroups.com
There is no "current application context."  You should have the pass a context into you.  If you can't do that, the only thing you can safely rely on is a context for just the base platform without being associated with an application.


--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Guy Rosen

unread,
Sep 29, 2010, 4:55:57 AM9/29/10
to android-platform
Thanks Dianne!

You mention I can obtain a "context for just the base platform without
being associated with an application" - where can I obtain one of
those?

- Guy.
> > android-platfo...@googlegroups.com<android-platform%2Bunsubscrib e...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-platform?hl=en.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com

Dianne Hackborn

unread,
Sep 29, 2010, 5:29:14 PM9/29/10
to android-...@googlegroups.com
ActivityThread.getSystemContext() I believe.

To unsubscribe from this group, send email to android-platfo...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

John Lombardo

unread,
Apr 18, 2011, 7:36:59 PM4/18/11
to android-...@googlegroups.com
According to Mark Murphy, ActivityThread is no longer available in the SDK (and indeed I can't find it). See http://stackoverflow.com/questions/3317520/android-app-activitythread-is-missing-in-current-sdk

Dianne Hackborn

unread,
Apr 19, 2011, 2:39:34 AM4/19/11
to android-...@googlegroups.com
Yes, but this list isn't for discussion about programming against the SDK.

On Mon, Apr 18, 2011 at 4:36 PM, John Lombardo <john.l...@gmail.com> wrote:
According to Mark Murphy, ActivityThread is no longer available in the SDK (and indeed I can't find it). See http://stackoverflow.com/questions/3317520/android-app-activitythread-is-missing-in-current-sdk

--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.

jinde song

unread,
Nov 13, 2013, 8:57:57 PM11/13/13
to android-...@googlegroups.com
I'm trying to get a context too.I 'm adding  features to my custom Rom. 
I just want to know if there is any bug this can raise?  

在 2010年9月28日星期二UTC+8下午11时51分01秒,Guy Rosen写道:
Reply all
Reply to author
Forward
0 new messages