Tuesday, June 14, 2011

BroadcastReceiver

As I'm moving from my old blog to blogger.com I'm re-posting some of the most interesting posts. See the original post at Blogger here.


This post caught a lot of visitors from Google searching for things like "android broadcast receiver tutorial" and the like. It's a pretty stupid and useless example, so I'll try to come up with a new one soon.



Here's the original post:


Let's start with my first code-tutorial. It's about BroadcastReceivers:

You can find an example in the SayMyName sourcecode.

It's not very hard to code: implement the method "onReceive()" and do what you want to do, when you receive an broadcast.

To make this work right, you have to modify your AndroidManifest.xml:
Write between the application-tag something like:

"android:name" sets the class, which is your broadcastreceiver.
With the "intent-filter"-tag you can control which broadcasts will get forwarded to your receiver.

With this example, your receiver gets the broadcasts "BOOT_COMPLETED" (which needs an additional in the AndroidManifest.xml) and "USER_PRESENT" (which gets sent when you unlock your phone by pressing the Menu-key).

A very tiny list of available broadcasts can be found in the Android API (search for "Standard Broadcast Actions")

That's it! :)

No comments:

Post a Comment