Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Application from Context

Is there a way I can get application from Context. I noticed that activity and service has getApplication() method but Context does not.

Background:

I am using/extending the GCMBaseIntentService and the

protected abstract void  onMessage (Context context, Intent intent) 

gives me a context as a parameter. I would like to get the Application object so I can downcast it to MyApplication and retrieve some variables

Thanks

like image 892
Snake Avatar asked Feb 10 '15 19:02

Snake


People also ask

How do I find application context?

You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.

What is context application?

It is the context of the current state of the application. It can be used to get information regarding the activity and application. It can be used to get access to resources, databases, and shared preferences, and etc. Both the Activity and Application classes extend the Context class.


2 Answers

Context#getApplicationContext will return your Application instance.

like image 200
Egor Neliuba Avatar answered Oct 19 '22 01:10

Egor Neliuba


val application : Application = context.applicationContext as Application
like image 8
eggham0518 Avatar answered Oct 19 '22 00:10

eggham0518