I want to make a service that I can register to. The service will broadcast messages of different "types" and each application can register itself to recieve messages of different type.
For example:
I want to write a servive that reads the twitter messages of some user and broadcasts to the systems the tags.
Then a consumer can register to recieve only messages of tag "foo", and recieve the tweet message.
Another consumer can register to recieve only messages of tag "bar", and recieve the tweet message.
Lets assume I know how to build a service. My first idea is to just broadcast a something, and then filtering it in the apps. But I am not happy about this solution. I know there are some android services that work similar to what I want, but I found no reference on the web on how to implement this.
Some RTFM I have done is:
http://groups.google.com/group/android-developers/browse_thread/thread/e1863d2822b22a33/90873ef925cd2aad
http://www.vogella.de/articles/AndroidServices/article.html
What is the simplest way to send message from local service to activity
How to have Android Service communicate with Activity -> not good for me, as this question is about a single process, and I need the service talking to several different processes
The main problem, is that most of the docs on the internet are about me calling the service, and not the service calling me.
Assuming you talk about Service.sendBroadcast(Intent): You can broadcast anything you want but it gets impracticable at some point. You could even define a new Action for each Twitter tag but that will just be complicated.
You could use the Data part of the Intents here, Receiving code could filter Broadcast like that:
IntentFilter ifilter = new IntentFilter("com.your.package.ACTION_TWITTER_MSG");
ifilter.addDataScheme("twitter");
ifilter.addDataAuthority("com.your.package", null);
ifilter.addDataPath("/foo", PatternMatcher.PATTERN_PREFIX);
and you send them like that:
Intent intent = new Intent("com.your.package.ACTION_TWITTER_MSG");
intent.setData(Uri.parse("twitter://com.your.package/foo"));
context.sendBroadcast(intent);
Related
Google says: "GCM delivers messages as a broadcast. The receivers must be registered in the manifest in order to wake up the application." but What component of Android does actually get the message first? Because İ dont think the Client app is the first component that gets the message by Broadcast receiver.
Would be happy if someone can explain it A bit more or give me A link to a source. Thx
The client app is really the first component to get this broadcast by a broadcast receiver. Not only that, it is the only component to get it, simply because nobody else can listen for that specific broadcast (the one that is defined in your application's manifest).
But I guess you wanted to know who gets the message from the network and sends this broadcast. That is Google Play Services.
Is it possible to mimic parse.com's push service hitting a broadcast receiver's's intent filter with MQTT?
Parse is incredibly unreliable and unscalable, I can't for see that service be able to handle possibly millions of pushes needed. But their feature to allow a push msgs to trigger an intent is invaluable.
So, As I setup up an MQTT service in android, how can I go about allowing incoming msgs to that service to then hit a broadcast receiver's filter, in turn hitting an intent.
Have a look at the code snippets in the page http://www-01.ibm.com/support/docview.wss?rs=171&uid=swg24033580&loc=en_US&cs=utf-8&lang=en ... Also download the zip that has more code.. You can actually use the process given there to send push notifications using MQTT.
I wondering if it is possible to create a private broadcast.
I actually register broadcastreceiver in my activity and use
sendOrderedBroadcast(broadcast);
method to send the broadcast.
But, for now, the intent (broadcast) used for this method is defined like this :
Intent broadcast = new Intent("com.mypackage.broadcast");
So every external application which declare this package name can listen what I am sending, and I don't want to.
So how to make this impossible, that no one can listen my broadcast ?
I think you are looking for LocalBroadcast Manager. The docs say:
It is a helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent). One of them is that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.`
See how to use LocalBroadcastManager? for more. Hope it helps you.
For LocalBroadcast Manager to work the app should be running.
To have a generic strategy to limit Android broadcasts to your own app only, we can do as follow.
Intent intent = new Intent();
String packageName = context.getApplicationContext().getPackageName();
intent.setAction(packageName + "<MY_ACTION>");
context.sendBroadcast(intent);
Since the application package would remain unique for all apps on android, this would safely limit the Broadcast to your own app and you can register BroadcastReceivers in AndroidManifest.xml.
I'm using C2DM the first time and I'm looking for a general advice how I can achieve the following:
Upon receiving a C2DM messages I decide:
- if the application is upon the current activity will display an "alert popup".
- if the application is not open I'd like to send a message to the notification bar (similar to new emails, sms, twitter etc.)
We have a GlobalBroadcastReceiver extends BroadcastReceiver which implements public void onReceive(Context context, Intent intent). This is the only receiver registered in AndroidManifest.xml.
So basically all our broadcasts are piped through this receiver and the first scenario is no problem.
However I'm, wondering how to tackle the second problem. How can I make sure I receive a C2DM.RECEIVE broadcast even when my application is closed and then: how can I notify the user about the incoming data?
I'm super confident there are already a lot of solutions out there but since I couldn't find them I think I'm just missing something of the bigger picture.
How can I make sure I receive a C2DM.RECEIVE broadcast even when my application is closed
Have your receiver registered in the manifest, per the C2DM documentation.
then: how can I notify the user about the incoming data?
Raise a Notification.
Since your receiver will not necessarily know if there is an activity of yours in the foreground, the best solution is to send your own broadcast Intent, but one that is ordered. Have the activity register a high-priority BroadcastReceiver for your own broadcast, and have another manifest-registered BroadcastReceiver implement a normal-priority BroadcastReceiver for your own broadcast. If the activity gets the broadcast, it displays your popup (ick) and aborts the broadcast. If your "backstop" BroadcastReceiver gets the broadcast, it displays a Notification. Here is a blog post with a bit more detail on this pattern, and here is a sample project demonstrating this use of ordered broadcasts.
I created a BroadcastReceiver and configured it with an android.provider.Telephony.SMS_RECEIVED action filter so it is called everytime the phone receives a text.
Is there some event/action or other way for my application to be notified whenever the phone sends a text (preferably independent of the application that sends it)?
So far the only option I see is to poll the content provider for content://sms/sent which doesn't even give me all sent texts because applications can choose not to put it there.
Unfortunately there is (currently) no way to implement a BroadcastReceiver because the standard sms application uses a SmsManger to send the messages but specifies concrete internal classes for the sent and delivered intents (SmsReceiver.class and MessageStatusReceiver.class respectively). Not that it is any consolation but you can find the following comment in the Sms application's source:
// TODO: Fix: It should not be necessary to
// specify the class in this intent. Doing that
// unnecessarily limits customizability.
The best alternative seems to be polling content://sms/sent, potentially using a ContentObserver.