Skip to content Skip to sidebar Skip to footer

Accessing Main Activity In Broadcast Reciever Without Using Intent

I am stuck in a problem. I have a broadcast receiver that calls the method of class takes in context and main activity reference in constructor. I dont know how to access main acti

Solution 1:

Try this hope it works:
in Main Activity:
publicclassMainActivityextendsActivity {

publicstatic MainActivity getInstance() {
    returnnew MainActivity();
}
}
And in your receiver:
MainActivity reference=MainActivity.getInstance();

Solution 2:

I dont know how to access main activity in broadcast receiver

You don't. If this is a manifest-registered receiver, you may not have an instance of this activity, anyway.

Either:

  • Register the receiver from inside the Activity, via registerReceiver(), in which case your nested BroadcastReceiver class can reference the outer Activity instance, or

  • Use an event bus (greenrobot's EventBus, LocalBroadcastManager, etc.) to publish your event, and have your activity subscribe on that bus (if the activity happens to be around)

Post a Comment for "Accessing Main Activity In Broadcast Reciever Without Using Intent"