Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting context in java android

Tags:

android

hey i have an android application in which i monitor gmail server in a thread in background..this thread runs infinitely ...on getting a mail in the mailbox I have to make a toast..but i am not able to get the context of current activity(since there are multiple activities in the app)...how to get context of current running activity or may be application..getApplicationcontext gives null.

like image 946
user741855 Avatar asked Nov 16 '25 13:11

user741855


2 Answers

If you're running an Activity or a Service, both are Context themselves, so calling this suffice. On any callback method for a listener, this will refer to the listener, not to the Activity. You can refer to the Context using any View:

Context context = myView.getContext();  
like image 185
Aleadam Avatar answered Nov 19 '25 02:11

Aleadam


In toasts and listeners and everywhere, if you need to get resources, You could use

Resources.getSystem().getString(android.R.string.cancel)

It has absolutely universal placement, but supports only system resources.

like image 23
Gangnus Avatar answered Nov 19 '25 01:11

Gangnus