Call an Activity Method from a Broadcastreceiver Class

Calling a Activity method from BroadcastReceiver in Android

try this code :

your broadcastreceiver class for internet lost class :

public class InternetLostReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
context.sendBroadcast(new Intent("INTERNET_LOST"));

in your activity add this for calling broadcast:

public class TestActivity  extends Activity{

protected void onCreate(Bundle savedInstanceState) {

registerReceiver(broadcastReceiver, new IntentFilter("INTERNET_LOST"));

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// internet lost alert dialog method call from here...

protected void onDestroy() {

Call activity method from broadcast receiver android?

I was able to solve it by declaring receiver programmatically:

In the activity befor sending the message

private void sendSMS() {
BinarySMSReceiver smsReceiver = null;
smsReceiver = new BinarySMSReceiver();
IntentFilter portIntentFilter = new IntentFilter("android.intent.action.DATA_SMS_RECEIVED");
portIntentFilter.addDataAuthority("*", "9512");
registerReceiver(smsReceiver, portIntentFilter);

String messageText = etMobile.getText().toString().trim();
short SMS_PORT = 9512;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendDataMessage(etMobile.getText().toString().trim(), null, SMS_PORT, messageText.getBytes(), null, null);

In receiver class

 public class BinarySMSReceiver extends BroadcastReceiver {
VerifyActivity vAct = null;

void setActivityHandler(VerifyActivity main) {
vAct = main;

public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;

if (null != bundle) {
String info = "SMS from ";
String sender = "";
String msg = "";

Object[] pdus = (Object[]) bundle.get("pdus");

msgs = new SmsMessage[pdus.length];
byte[] data = null;

for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sender += msgs[i].getOriginatingAddress();
info += msgs[i].getOriginatingAddress() + "\n";

data = msgs[i].getUserData();

for (int index = 0; index < data.length; ++index) {
info += Character.toString((char) data[index]);
msg += Character.toString((char) data[index]);

Log.e("message", "receiver " + msg);
Log.e("sender", "from " + info);
vAct.msgReceived(msg); //activity method

Unregister the receiver

protected void onPause() {

Call an activity method from a BroadcastReceiver class

Create the BroadcastReceiver dynamically:

In your BroadcastReceiver class define class member:

YourMainActivity yourMain = null;  

and method:

setMainActivityHandler(YourMainActivity main){
yourMain = main;

from your MainActivity do:

private YourBroadcastReceiverClassName yourBR = null;
yourBR = new YourBroadcastReceiverClassName();
IntentFilter callInterceptorIntentFilter = new IntentFilter("android.intent.action.ANY_ACTION");
registerReceiver(yourBR, callInterceptorIntentFilter);

finally, when yourBR.onReceive is fired you can call:


call activity method from broadcast receiver

Thanks @Manishika. To elaborate, making the Broadcastreceiver dynamic, instead of defining it in the manifest, did the trick. So in my broadcast receiver class, i add the code :

MainActivity main = null;
void setMainActivityHandler(MainActivity main){

In the end of the onReceive function of the BroadcastReceiver class, I call the main activity's function :


In the main activity, I dynamically define and register the broadcast receiver before sending the sms:

SmsReceiver BR_smsreceiver = null;
BR_smsreceiver = new SmsReceiver();
IntentFilter fltr_smsreceived = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

Calling Main activity method in broadcastReceiver

This is because when your broadcast receiver executes your MainActivity's method, then might be chances that your activity is not running or not even starts, so this kind of problem occurs.

How to solve:

  1. Create one global class.
  2. Made your showNotification method public static there, if you want then pass the context.
    3.just call it from main activity as well in broadcast receiver.

If you are unable to make static method, then just create object of that global class, and call that method using object.

Tell me if you face any problem while implementing this.

Call Activity Method from BroadcastReceiver

Your Activity will likely be long killed and dead by the time the AlarmManager triggers your BroadcastReceiver. When that happens, Android will create a new process for your application, instantiate your BroadcastReceiver and call onReceive(). This is why the variable viewScheduleActivity is null.

As @Athena suggested, you don't need to call back into your Activity in order to cancel alarms.

Calling a activity method from a BroadCast Receiver

Your Context context; of the MainActivity is never assigned,

change your code to this

public static void additionDone(int number, Context context){
...//work with that context

and in your reciever

MainActivity.additionDone(1, context); //call with the context you got in your receiver constructor

Related Topics

Leave a reply