Home » Android » android – How get Activity form BroadcastReceiver in onReceive

android – How get Activity form BroadcastReceiver in onReceive

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&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.

Answer:

You can communicate broadcast to activity from this:-

   Intent intent=new Intent(context,YourActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           context.startActivity(i);