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.
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();
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.
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