Archive for 29 April 2013

Notification buttons

Notification buttons

This tutorial shows how to make an audio playback service and how to control audio playback using buttons on a notification. I am going to assume that you have some knowledge of android app development, and know your way around Eclipse, or other SDK used for Android. This is my first tutorial, so please let me know in comments if anything needs to be explained in more depth, or point out any mistakes made.

1. Create service

First things first, we need to create the service that will play music. You can do that by creating a new class that extends Service. You will also need an instance of MediaPlayer. Your service should also implement OnPreparedListener and OnCompletionListener interfaces to receive callbacks from the MediaPlayer. Your code should look similar to this:

onCreate() is called when your service is first started. We’ll use it to instantiate the MediaPlayer, and set listeners. onStartCommand() is called every time you launch your service from an activity – this could happen several times during service’s lifetime. Returning START_STICKY causes your service to stay alive after onStartCommand() finished executing.
You should also define your service in your project’s manifest. Put the following between the <application> tags:

2. Let’s play some music

Now that we have the service, let’s make it play the music. We will modify onStartCommand() to handle the incoming intent, and override onDestroy() to stop the music when service is stopped:

This will get the url from intent and use it as a data source for media player. You can use this to stream media from the internet. the prepareAsync() method starts buffering the audio stream in a secondary thread, and will call onPrepared() when finished – that’s where you start playback by calling start() on the MediaPlayer.

If you want to stream music from the internet, don’t forget to request the INTERNET permission inside the <manifest> tags of your manifest:

Next we’ll create an Activity (you should know how to do this) that will start the service. Add 2 buttons the layout: first button will start the service and play music, while the other one will stop the service:

The code above will start the service with url link provided. It should start streaming Radio Nova. The app should work at this stage. Go on and try it!

3. Show the notification

The final part of this tutorial is creating the notification that will show when service is running and present user with playback control buttons. Let’s create a showNotification() method inside our Service. The notification will have Stop and Pause Buttons. You can add buttons by addAction(). We should also define the broadcasts for those buttons.

We will use NotificationCompat class from the support library for building the notification so it’s backwards compatible. If you want you can use the Notification class, however it’s not recommended.

To make my life easier, I made the createPendingIntent() method to create broadcast pending intents easy:

startForeground() method will bring your service to the foreground by showing the notification.

Notification should be shown whenever new stream is played:

Now all that’s left is handling the button presses. To do that, we need to create a BroadcastReceiver, register it inside onCreate() method, and unregister when service is destroyed:

At last, we should implement the onCompletion() method. It is called when song finished playing. In my implementation it simply stops the service:

That’s it! You can now test the app. When you start the audio, you should be able to see this in your notification shade:

Notification buttons

Of course, the app is a bit rough around the edges; the “pause” button doesn’t turn into “play” when you press it, but I will leave this as an exercise for the reader.

Full source code:



This Android app created for uses the new Google Maps API v2 and notifications for playback controls. Stories can be streamed or downloaded beforehand. The app features rambles – Google Maps API is used to find optimal route between 2 points with stories in-between. The rambles are automatically downloaded from server, but user can create their own ramble by selecting the start and end point. It also handles links, so they open in the app instead of web browser. It’s designed to look clean and be easy to use.

Storymap app is now released and can be downloaded in Google Play: Storymap Android


Notification is shown while story is being played back.

Notification is shown while story is being played back.


I made this little program to test my sudoku-solving algorithm around the christmas time. I used XNA to draw the puzzle, and solving algorithm works in a separate thread (included below). This is not the best algorithm since it can’t solve “hard” puzzles. But it can logically deduct numbers (no guessing) and put them in the right place.



HTML Editor

I remember making this program back in 2004. Still, I was very surprised today when I started it and noticed how developed it is. It has an awful lot of features for something that I knew I would not use anyway. Obviously it doesn’t offer syntax highlighting but has other useful features for novice web designers, like html templates. It allows to easily add html tags and has color pickers. Unfortunately no source code.

The readme.txt file reads:

Aplikacja do edycji stron HTML. Nie obsluguje
kolorowania skladni, ale posiada wbudowane
szablony znaczników. w kazdym razie jest
nieco lepszy od standardowago notepada :)

Which basically explains that it’s a simple program, but still better than windows notepad.


Date to Date

This program made in 2004 (I was 16 at the time) shows number of days, weeks, months and years between two dates. Unfortunately (or luckily) I don’t have the source code anymore due to the hard disk crash somewhere between 2004 and 2006. Did I mention this program is in Polish?

Date 2 Date