How to update UI when an alarm broadcast receiver receive the event? - android

In my application there's a main activity which might be show on the top or in the background, and there's a broast receiver who receive the alarm event.
My main activity and the receiver seems to be two instances, and I can't call a static member in main activity (e.g.listview) to update itself from the receiver. It this correct?
So, how do i notify my current main activity to update itself?
p.s. I don't want to show my main activity if it's not currently on top.
Thanks a lot.

One mechanism I have seen used to provide a good solution is a Broadcast Receiver. You can read more about the solution in the Common Tasks area of the android documentation.
Edit: Although, rereading your question, I'm not positive I have enough information. If your receiver is already in your activity, then you just need to use the runOnUi(Runnable) call from the Activity to allow you to update the UI.

Related

What is the proper way to communicate between an Activity and a BroadcastReceiver?

I have a BroadcastReceiver which listens to system intents such as Intent.ACTION_PACKAGE_ADDED. When it receives one, the Activity needs to refresh its the data it presents.
Of course, the Activity may not have focus, or may not even be running when this happens. What is the proper way to "leave a message" for the next time the Activity gains focus?
I thought of storing a boolean in the SharedPreferences, but something tells me this is not the right way to go.
What is the proper way to "leave a message" for the next time the Activity gains focus?
IMHO, you have four separate issues here:
Issue #1: How does the BroadcastReceiver tell a running Activity that an event occurred that may be of interest?
Solution: Use an event bus (LocalBroadcastManager, greenrobot's EventBus, Square's Otto), and post a package-added event from the receiver on the bus. Your activity can be registered for events from the bus while it is in the foreground, and it can update its view on the data when your event is received.
Issue #2: How does the BroadcastReceiver update the model data in the SQLite database when the broadcast occurs?
Solution: Delegate this, plus the receiver's side of the event bus logic, to an IntentService, as you need a background thread to do the database I/O. Post the event on the bus when the work is completed.
Issue #3: So, what happens if my activity is not in the foreground at the time the event goes out, but the activity exists (i.e., is in the background)?
Solution: Either reload your data in onResume() or use some sort of a "sticky" event with the bus. LocalBroadcastManager does not offer that, but greenrobot's EventBus has the notion of sticky events, and Square's Otto has a related #Producer construct.
Issue #4: What happens if I do not have an activity instance at all? For example, the package is added when my process is not running, so Android forks a process for me and invokes my receiver, but I have no UI code at all in my process right now?
Solution: Do nothing special. Your existing "load-the-data" logic will handle this case.
One simple solution can be a static variable flag in Broadcastreceiver. Once receiver receives intent, make flag=true.
Activity when gains focus can look at this static variable and can refresh data accordingly and turn this static flag to false.
It depends of what you are trying to do when the Intent is received. If you only want to refresh the content, the the proper way is to use the onResume and onStopto attach the broadcast receiver listener. This will only work while the activity is on the foreground, so every time the user enters the activity again you need to reload the list to avoid the case that this intent arrived while not in foreground.
Another way to proceed if you want to listen it while you are not showing your UI is to create a Service that listens to this Intent with the BroadcastReceiver. Again it depends of the moments you need to receive this event.
To sum up:
For foreground UI: onResume to attach and onPause to detach.
For background: use a Service

Is it possible to trigger the onclick event of activity in background service?

For example, I started an activity, there is a button in the activity
And then I start the background service, this service will check whether the target activity is on foreground, then trigger the click event of that button.
Is it possible to do this?
Why would you trigger a click event of an activity rather than notifying the activity about its results? Try to use local broadcasts which are sent from the service to the activity. The activity registers for that broadcast and in its onReceive of the BroadcastReceiver you'll trigger your onClick method or any other method of your choice. The Receiver is registered in onResume and unregistered in onPause to guarantee that the activity is actually visible.
I would not recommend to use a direct dependency on your activity as this might cause IllegalStateExceptions if in any circumstance your activity is not started or visible at all.
I can strongly recommend you to use eventbus concept like "otto by square" in this case.
you will subscribe to event from the activity this will keep to modularity and will let you do this function

Android service (location listener) to update activity

I have an activity that shows a map with markers on it. Also I have a location listener, whose values will update the activity arraylist and cause a redraw of the markers based on the updated arraylist values.
I want this to always run (even if the activity is put in the background). I understand that Activity may get killed so I expect that I need a service for the location lisntener. Now my question is: How can I update the activity arraylist and redraw contents when the service is actually the one obtaining info?
I read something about boundservice so maybe I need that? However I need the service running even if activity is killed.
OR Do I just create service and then send a broadcast to activity?
Thank you
Like you have written at the end of your post you could use a BroadcastReceiver for this. That's the way I use it.
In onCreate(...) of your Activity, register it locally:
LocalBroadcastManager.getInstance(this).registerReceiver(...);
in the onReceive(...) method of your receiver, update the required data and unregister it in Activity's onDestroy() method, again using the LocalBroadcastManager object.
Needless to say, you send the broadcast signal from your Service's onLocationChanged(...) method, since it is in the same package as your Activity, the LocalBroadcastManager will forward the signal to your Activity.
LocalBroadcastManager.getInstance(this).sendBroadcast(...);

Android: How to notify activity of an other activities finish()

I do have a activity, which I don't have access to (we're working at an AIR ANE). From this activity, the one we're working on is triggered, but we can't use onActivityResult, as we don't have access to the calling activity. How can I work around this? If I close the second activity via finish(), it's to late to send an event. If I sent it before, the underlaying activity is still paused, as I've understood. I thought about using an Handler with a postDelayed, but I think, the timing will make this approach messy. Is there something like didFinish(), that's called once the next activity has already resumed?
Any thoughts appreciated!
Thnx, Marcus
The easiest way would be to send a broadcast before calling finish() and to register a BroadcastReceiver where you want to be notified.
You can use toast,alert or even broadcast receiver before calling finish();

How to update textview of paused activity

I'm receiving intents in a broadcast receiver (declared in manifest), details of which I am 'logging' via b'cst intents received by my 'MainActivity', whose receiver updates the contents of a text view and is registered within the MainActivity code.
I'm wondering if there's a way to keep the contents of the MainActivity text view updated even if the MainActivity doesn't have 'focus' (i.e. another activity has been started). I appreciate that the b'cast receiver of the MainActivity will become unregistered upon pausing, but feel there ought to be a way to do this.
Any ideas?
You should do all of that on the "onResume" of the activity, just when it gets to the foreground.
One of solutions is to use a handler inside receiver, and in that you call a static method inside your activity...inside that method you can update or whatever you need to do with your text view...

Categories

Resources