sendBroadcast() - Should it be called inside Activity? I am trying to call sendBroadcast() from my method of utility-class which doesn't extend Activity. I am getting compilation error as below
The method sendBroadcast(Intent) is undefined for the type MyWrapperClass MyWrapperClass.java
Here is the code snippet:
abstract class MyWrapperClass {
public static void sendData()
{
Intent intent = new Intent ("com.proj.utility.mgr",null);
intent.putExtra("example","Broadcasting ");
sendBroadcast(intent);
}
}
Is there any concept behind using sendBroadcast call inside my class. There is no issue in using sendBroadcast() inside Activity. Can someone here help me to resolve it? Or Any other suggestions are invited to return data from utility class to application asynchronously. Thanks in advance.
The sendBroadcast() method allows to send Broadcast Intents. You cannot trigger system Broadcasts, the Android system will prevent this. But you can define intent-filters for your own actions and trigger them via the sendBroadcast() method.
A Sticky Broadcast is a Broadcast that stays around following the moment it is announced to the system. Most Broadcasts are sent, processed within the system and become quickly inaccessible. However, Sticky Broadcasts announce information that remains accessible beyond the point at which they are processed.
Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.
An activity specifies a layout to represent it on screen. Intent. An intent is a system message. It can be broadcast around the system to notify other applications (or your own!) of an event, or it can be used to request that the system display a new activity. Follow this answer to receive notifications.
You should pass the context from activity class to utility class to access the specific application resources like startActivity, sendBroadcast, etc.
context.sendBroadcast(intent);
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