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
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.
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.
Context#getApplicationContext
will return your Application
instance.
val application : Application = context.applicationContext as Application
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With