Google Cloud Messaging: BroadcastReceiver or Service? - java

What is the generally preferred method of using GCM in its current state?
The documentation only talks about using it with a BroadcastReceiver and only mentions Services in one sentence without further explanation.
In my application, I need to be able to react to an unknown number of successive GCM messages and queue them so I can process them one by one. This processing needs to be done in order the messages are received in.
This cannot be done with a BroadcastReceiver, as for every broadcast received, a new instance of my receiver class is created (this was the method I tried first as per the getting started guide). Can it be done with a service or, more precisely, is the instance of my service kept between messages received?
If so, when and how does this service need to be started, added to the manifest, etc.?
I do not need to interact with my main application/UI. The service can do its business on its own.

GCM message comes as broadcast so you must use BroadcastReceiver. If you need to queue them for any reason. just make your BroadcastReceiver hand the message to IntentService or anything else you find suitable for your task.

Related

How to get the Activity instance of an Android app running the foreground?

How do I get the actual Activity instance of the top current activity of a running Andoind application?
Reason: I receive a OnMessageReceived Data payload from Firebase while my application is running in the foreground, and I need to finish() it.
Note: there are tons of other info to get the ComponentName using getRunningTasks() or getAppTasks(), but all these does not seem to provide any way to get the actual Activity instance.
The better way would be to use a Local Broad Cast to inform your activity to finish itself.
You just need to register a Broad Cast Receiver inside your activity and then send the broadcast in your FCM Messaging Service. Check this page on how to do this.
Note: Make sure to unregister the receiver when you're done with the activity.
You can also use EventBus for the Same

Sending continuos data from service to activity at really very high speed

I am developing a testing application on an Architecture which is based on Producer-Consumer structure. I have an producer-consumer* problem, especially if an producer callback mechanism is utilized in android service i.e. consumer. Consumers are not supposed to hold the call for more than the minimum necessary time to have the info handed over. Since the producer’s callbacks are supposed to run in a different thread than the consumer’s one.
In my specific case within the callback of Producer only a reference moving of the passed object should be done and release the control right away. The object has to be consumed in the consumer thread. Currently I have been calling a method which only gets data coming within callback and processes that data and return it via Intent baack to the Android Activity.
Now, Android intents are well known to be resource consuming entities which are not meant (and not supposed) to be used to transfer data streams.
Within the Test app, one intent per callback is generated. Those overflow the whole system. For example, at 25% of load a traffic of about a thousand Android intents per seconds are triggered.
I want a way which doesn't include Android Intents(without any Thrid party jar) using which I can send data back to my android activity or route on host machine at super high rate so that my producer call back doesn't get crashed.
Use a socket connection between the Service and the Activity for streaming data. Intent is the wrong technique.

[Android]Does Android System gerrentee the execution order of user defined Broadcast Receiver according to their android:priority?

I designed a component(packed in .jar file) that contians a service, and this component could be contained in many applications(let's say there might be many applications who all have that component in them, are installed on one device). But only one instance of the component is expected to be exist on one deivce, that means if component in 'A' application has been started, the component in 'B' application is not going to be started during 'A''s lifecycle.
And maybe some day, there is another new application is installed on that device, but with a new version of that component. What I want is make sure the started component is always the newest version.
So, I want to use the BroadcastReceiver and android:priority as the solution(the new version will has a higher priority). But I have to make sure whether Android System gerrentee the sequence of excution(of BroadcastReceiver.onReceive) according to their priority.
Any one give me a hint? That would be very thankful:)
In short: yes if you use ordered broadcast.
According Android doc :
Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order.
So you must define the android:priority and you must send an ordered broadcast using : Context.sendOrderedBroadcast
Note that, one of your receiver can even abort the broacast so that it won't be propagated to the next receiver. (see : abortBroadcast)

Alert dialog display even when the application is not running

I want to notify the user of my app when he receives special news (from my app).
How can i display some kind of a messeage to the user, even if my app is running in the background (or not running at all if possible).
I want him to be notified by text, and sound.
Thanks.
You need to write a service for doing this. Then in your service code use Notification class to show text and sound alerts.
Use Broadcast Receiver, to notify abt some action. Like updates, sms, or abt indicate when booting of a phone completes,etc... BroadCast Receiver works on the principle of Publisher and Subscriber pattern.
If your app is not running and you want to do something you'll need some kind of broadcast receiver to receive a trigger.
Probably what you'll end up doing is starting up at device boot, to schedule some stuff. This answer here should get you into the right direction
After that, the best way to notify the user is with notifications (or Toast messages if you're a great fan;))
Have a look at
Status Bar Notifications

determine when background service has completed

currently, i'm starting a background service using Intent in my app, but is there anyway of determining when the service has completed?
..possibly by using some kind of listener, or sending a message from the service, to the activity??
First, activities and services can communicate: the easiest is to use a custom broadcast.
Second, if your service is short lived and is outlived by activity invoking it, then you might instead consider using AsyncTask to simply run your task in the background thread.
The usual trick you can use here is create a BroadcastReceiver in the Activity that listens for a specific Intent and then when the Service stops itself fire off that Intent.
You can send a broadcast Intent just before the service termination and implement a broadcast receiver.

Categories

Resources