0

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:

The main problem, is that most of the docs on the internet are about me calling the service, and not the service calling me.

Community
  • 1
  • 1
elcuco
  • 8,948
  • 9
  • 47
  • 69
  • BTW: better title to this question will grant you a free re-load on "DuckDuckGo"s homepage :) – elcuco Mar 12 '12 at 14:38
  • You should have a look at [Intents and intent broadcasts](http://developer.android.com/guide/topics/intents/intents-filters.html), both how to send them *(in this case from a Service)* and how to receive them with a [BroadcastReceiver](http://developer.android.com/reference/android/content/BroadcastReceiver.html). This is a very important android concept. Alternatively check out [ContentProviders](http://developer.android.com/guide/topics/providers/content-providers.html). They should also be useful here, but I didn't do much with these myself yet, so take that with a grain of salt. :) –  Mar 12 '12 at 14:46
  • @alextsc so you tell me to create a message to the service, and the service will contact "me" via intent? That was one of my options. Make it a full answer so I can +1 it. EDIT: well, I thing intents are too slow for me. I need something faster. – elcuco Mar 12 '12 at 14:53
  • @eluco Yes pretty much. No time right now, as long as it helps it's ok. Don't need any more rep. :P Intents are generally fast (instant) unless you send a lot of data with them (somewhere above the 1 MB range, which creates a lot of overhead for the system). In this case rather send an intent that basically says "hey, new data". The receiver can pull the data from a shared location after that (e.g. a SQLite db/file or a ContentProvider). –  Mar 12 '12 at 16:35

1 Answers1

0

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);
elcuco
  • 8,948
  • 9
  • 47
  • 69
zapl
  • 63,179
  • 10
  • 123
  • 154