Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a Activity method from BroadcastReceiver in Android

Here I am creating an online application that depends only on Internet.

So whenever there is a network error it must notify user. For that, I have created a BroadcastReciver that receives call when network connection gets lost(Internet).

All this works perfectly. Now what I need is that I have to call a method of Activity from this Broadcast Receiver, where I have created an Alert Dialogue.

I have read many answers on stack-overflow.com that I can declare that method static and call by using only Activity name,

e.g MyActivityName.myMethod()

But I can't declare my method static, because I am using Alert Dialogue there and it shows me error on line,

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

that Cannot use this in a static context.

So, how can I call a method of Activity(must not static and without starting that activity) from a Broadcast Receiver ?

And can I get Activity(or fragment) name from Broadcast Receiver which is currently running?

like image 215
Jay Vyas Avatar asked Mar 07 '14 04:03

Jay Vyas


People also ask

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 is the method name in BroadcastReceiver receive the message?

To receive SMS messages, use the onReceive() method of the BroadcastReceiver class. The Android framework sends out system broadcasts of events such as receiving an SMS message, containing intents that are meant to be received using a BroadcastReceiver.

What is the use of BroadcastReceiver in Android?

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.


2 Answers

try this code :

your broadcastreceiver class for internet lost class :

public class InternetLostReceiver extends BroadcastReceiver{
@Override
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{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

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

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(broadcastReceiver);
}
}
like image 157
Vijju Avatar answered Nov 07 '22 22:11

Vijju


INTERFACE: Keep BroadCastReceiver and Activity code separate!

You can make a CallBackListener interface. The interface will work as a bridge between BroadcastReceiver and Activity.

1) Create a CallbackListener

interface ConnectionLostCallback{

      public void connectionLost();

} 

2) Provide ConnectionLostCallback in your BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver{

     private ConnectionLostCallback listener;

     public MyBroadcastReceiver(ConnectionLostCallback listener ){

           this.listener = listener     //<-- Initialze it

     }

     @Override
     public void onReceive(Context context, Intent intent) {

           listener.connectionLost();

     }
}

3) Implement the ConnectionLostCallback in your Activity and override the method

YourActvity extends AppcompatActivity implements ConnectionLostCallback{

    // Your Activity related code //
      //    new MyBroadcastReceiver(this);  <-- create instance

    private void showAlertMessage(){
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    } 


    @Override 
    public void connectionLost(){

         showAlertMessage();          //<--- Call the method to shoe alert dialog

    }


}

Relevant link:

If you want to know how to make a BroadcastReceiver independent of any activity ie How can you reuse the same BroadCastReceiver with different Activities? Then READ THIS

like image 43
Rohit Singh Avatar answered Nov 07 '22 22:11

Rohit Singh