Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get Activity form BroadcastReceiver in onReceive

I define BroadcastReceiver in an application manifest and the receiver receives events in onReceive as expected. However I do not create the receiver class so I can't pass any information about my activity which the receiver is supposed to control. Context parameter gives me only application context and no any activity reference. So what is right way to make communication between application activities and broadcast receiver?

like image 436
Don Cesar D'Bazar Avatar asked Jul 16 '13 05:07

Don Cesar D'Bazar


People also ask

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.

How do I check if BroadcastReceiver is registered?

Currently there is no way to check if a receiver is registered using the receiver reference. You have to unregister the receiver and catch the IllegalArgumentException that is thrown if it's not registered. This is ugly, and a boolean method to check if it's registered would be helpful.

How do I send data from BroadcastReceiver to activity?

Step 1. Open your project where you want to implement this. Step 2. Open your BroadcastReceiver class from where you pass data to activity inside your onReceive() you need to start intent and pass data inside intent and start sendBroadcast() as shown bellow.

What file is used to register the BroadcastReceiver?

Register Broadcast: Statically (manifest-declared) - This receiver can be registered via the AndroidManifest. xml file.


1 Answers

The answer is a little twisted but I found it here as well Inform Activity from a BroadcastReceiver ONLY if it is in the foreground It seems working. Please close the question then.

like image 178
Don Cesar D'Bazar Avatar answered Oct 18 '22 02:10

Don Cesar D'Bazar