Since fragments are intended to encapsulate logic I decided to put BroadReceiver logic in my own fragment.
At first I setup an Alarm like it was shown in ApiDemos:
Intent intent = new Intent(getActivity(),OnAirPresentBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
OnAirPresentBroadcastReceiver receives the broadcast and the purpose of this broadcast is to update the fragment’s UI. I’m stuck here – is it possible and how ?
From your Receiver you can post another Intent to your Activity. From the Activity you can access the Fragment with the FragmentManager.
It’ll you’ll get it on your Activity in:
If your Activity is not running you’ll have Extra’s to your intent in OnCreate
To make sure your Activity is in foreground: (this code get the foreground Activity)
Another direction you can try:
Now the event you come in your Activity’s onNewIntent function, if its active. That’s not a full solution but that could be a nice direction.