Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to application class in Broadcast Receiver

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?

like image 986
Dr.jacky Avatar asked Jul 18 '14 13:07

Dr.jacky


People also ask

What class do you have to inherit from to be a broadcast receiver?

Receiving components of the broadcast intent will need to inherit from a Receiver class available in the Android SDK. These receiving components (broadcast receivers) then need to be registered in the manifest file as a receiver that is interested in the broadcast intent.

What is application broadcast?

BROADCAST APPLICATIONS means, with reference to a CCC semiconductor and software products, use of such product in any real-time video encoding or transrating infrastructure applications serving satellite DTH, video contribution, video distribution, digital -------- *** Portions of this document have been omitted ...

What is the role of the onReceive () method in the BroadcastReceiver?

Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.


2 Answers

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());
 }
like image 84
Lalit Poptani Avatar answered Sep 29 '22 03:09

Lalit Poptani


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.

like image 23
j2esu Avatar answered Sep 29 '22 04:09

j2esu