Calling a Activity method from BroadcastReceiver i

2019-01-14 06:28发布

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?

3条回答
贪生不怕死
2楼-- · 2019-01-14 06:53

Add a boolean variable in you activity from where you are open alertdialog

boolean isDialogOpened = false;

// in broadcast recever check 
if(isDialogOpened) {
    alertDialog();
}

And replace your code for alertdialog with this one

public void alertDialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    alertDialog.setMessage("Network not found.");
    alertDialog.setPositiveButton("Check Setting",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });

    alertDialog.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            isDialogOpened = false;
        }
    });

    alertDialog.setOnCancelListener(new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            isDialogOpened = false;
        }
    });

    alertDialog.show();
}
查看更多
神经病院院长
3楼-- · 2019-01-14 07:09

Pass your Activity's context to BroadcastReceiver's contructor.

public class ResponseReceiver extends BroadcastReceiver{

    MainActivity ma; //a reference to activity's context

    public ResponseReceiver(MainActivity maContext){
        ma=maContext;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ma.brCallback("your string"); //calling activity method
    }

}

and in your MainActivity

public class MainActivity extends AppCompatActivity {
    ...
    public void onStart(){
        ...        
    ResponseReceiver responseReceiver = new ResponseReceiver(this); //passing context
    LocalBroadcastManager.getInstance(this).registerReceiver(responseReceiver,null);
        ...
    }

    public void brCallback(String param){
        Log.d("BroadcastReceiver",param);
    }
}

hope it helps

查看更多
Animai°情兽
4楼-- · 2019-01-14 07:10

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);
}
}
查看更多
登录 后发表回答