Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inform Activity from a BroadcastReceiver ONLY if it is in the foreground

Tags:

Maybe it's easy, but I couldn't really figure this out right so far... I got a BroadcastReceiver waiting to get triggered by the AlarmMangager - this works fine.

Now: because the event, if it occurs, needs to refresh some elements on screen of the main Activity, I would like to send an Intent from that background BroadcastReceiver to my Activity - but only if it is currently in the foreground, aka active.

If it is not running or not visible, I don't care - and the last thing I want to do is start the Activity by my intent! I handle repainting of the views in my onResume() method, so I don't care at all.

Any hints on how to do that? Thanks!

EDIT: my BroadcastReceiver is waiting for alarms that must be notified to the user. So, it must be there and declared in the manifest. The problem is: it will have to decide whether the mentioned Activity is currently up in front or not.

like image 914
Zordid Avatar asked Feb 17 '10 16:02

Zordid


People also ask

How do I send data from BroadcastReceiver to activity?

Intent intent = getIntent(); String message = intent. getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme.

Can broadcast receiver run in background?

If you want a background receiver, you need to register it inside the AndroidManifest (with intent filter), add an IntentService and start it when you receive a broadcast in the receiver.

What is the role of the onReceive () method in the BroadcastReceiver?

Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.

Which method will be called when intent broadcast shall be received by BroadcastReceiver?

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.


1 Answers

I believe that you're familiar with AlarmManager now (creating a new Alarm, register a receiver...) so I will not talk about that. Just give you a solution for your question.

Instead of registering a BroadcastReceiver in a class file and in manifest, you only create a new BroadcastReceiver in your activity, and then, register it in onResume method, and unregister it in onPause method, sth like this in your activity:

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {     @Override     public void onReceive(Context context, Intent intent) {       //do something            } };  @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      mIntentFilter = new IntentFilter();     mIntentFilter.addAction("your alarm action");     ... }  @Override protected void onResume() { registerReceiver(mIntentReceiver, mIntentFilter);     ... super.onResume(); }  @Override protected void onPause() { unregisterReceiver(mIntentReceiver);     ... super.onPause(); } 

The receiver will only receive the alarm intent when your activity is in foreground :)

(Sorry if my English is not clear)

like image 157
Binh Tran Avatar answered Dec 04 '22 18:12

Binh Tran