14

I want to check internet connection in Broadcast Receiver; And set result (A boolean flag) to a global variable, to use it on whole application, in if conditions; That if internet is disconnected, set a status imageview in main activity, to red image, and if connected, set it to green.

I followed this topic. But there is no getApplication() in Broadcast Receiver; And iI should use getApplicationContext() instead.

On another side, this topic:

when writing code in a broadcast receiver, which is not a context but is given a context in its onReceive method, you can only call getApplicationContext(). Which also means that you are not guaranteed to have access to your application in a BroadcastReceiver.

  1. What are the concerns?

  2. How can I access to my application class in broadcast Receiver?

  3. Is there better solution to check internet connection, set global variable and change my status imageview?

Community
  • 1
  • 1
Dr.jacky
  • 3,341
  • 6
  • 53
  • 91

2 Answers2

38

You can access your Application class in BroadCastReceiver by using its context,

 @Override
 public void onReceive(final Context context, Intent intent) {
   MyApplication mApplication = ((MyApplication)context.getApplicationContext());
 }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • So what is the quote's point? "you are not GUARANTEED to have access to your application in a BroadcastReceiver" ?! – Dr.jacky Jul 18 '14 at 13:07
  • Ofcourse you does have access to Application class in BroadCastReceiver. – Lalit Poptani Jul 18 '14 at 13:15
  • Is this mean quote is not true? By the way, does my scenario to receive and change network status is correct totally? – Dr.jacky Jul 18 '14 at 13:20
  • I am never using `getApplication()` to get the instance of Application class and always using `getApplicationContext()` to get instance of Application, so its guaranteed 100% – Lalit Poptani Jul 18 '14 at 13:23
  • Ok, but how can i change a imageview in main activity, when internet connection changed? – Dr.jacky Jul 18 '14 at 14:02
  • thats a different topic/question that you asked in your question, but still I will just give a hint of having a listener in your Activity which gets fired when Internet comes or goes. – Lalit Poptani Jul 18 '14 at 14:10
  • By listener, do you mean broadcast receiver? – Dr.jacky Jul 18 '14 at 14:57
  • 10
    That currently the object returned by getApplicationContext happens to be the application instance itself is an implementation detail that most certainly is NOT guaranteed by the API. – Cumbayah Mar 14 '15 at 20:58
  • 4
    To guard against ClassCastExceptions, you can check: `if (context.getApplicationContext() instanceof MyApplication) {` – friederbluemle Apr 05 '16 at 07:16
  • @LalitPoptani any comment in response to cumbayah's comment and https://stackoverflow.com/questions/5018545/getapplication-vs-getapplicationcontext/6760019#6760019 – Shubham AgaRwal Apr 01 '19 at 11:35
3

Maybe it will help somebody. If using own application class:

public class App extends Application {

    private static App sInstance;

    public static App get() {
        return sInstance;
    }

    @Override
    public void onCreate() {
        sInstance = this;
        super.onCreate();
    }

}

Then you can use App.get() in your broadcast receiver. According to onCreate() docs it will be called before receiver calls.

Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.

j2esu
  • 1,647
  • 1
  • 16
  • 26