NOTE: Of late, I have been getting requests for very trivial problems that many of you are facing in your day-to-day work. This blog is not to solve your "project" problems - surely not a "Support" site.
I just love to share my knowledge in my spare time and would appreciate any questions or feedback on the articles and code I have shared. I also do appreciate thought-provoking questions that would lead me to write more articles and share.
But please do not put your day-to-day trivial problems here. Even if you do, you most probably would not get a response here.

Search This Blog


Tuesday, 22 September 2009

Service and Notification | Android Tutorial for Beginners – (Part 8)

Now that we have worked with services and notifications separately, we are all ready to use the two together to make a real service that is notified to the user through a status bar update that the service is running. The status bar removes the notification as soon as the service is stopped.

In this program, I also increment a counter once the service starts to show that the service is continuously running in the background while we do other work. When we stop the service we are able to see the updated count.

Let us look at the code:

I have created a class NotifyService which extends the service class. This is the service class that will be started through the activity – ServiceLauncher

Now let us see what the service does?

It does 3 things – Toast a message that the service has started. Update the status bar with a notification. Finally start incrementing the counter. All this is done in the onCreate() method of the NotifyService Class

Toast.makeText(this,"Service created at " + time.getTime(), Toast.LENGTH_LONG).show();

Here is the showNotification() Method:

    private void showNotification() {
 CharSequence text = getText(R.string.service_started);
 Notification notification = new Notification(, text, System.currentTimeMillis());
 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, ServiceLauncher.class), 0);
notification.setLatestEventInfo(this, getText(R.string.service_label),
      text, contentIntent);
nm.notify(R.string.service_started, notification);

Finally, here is the method for incrementing the counter incrementCounter():

    private void incrementCounter() {
timer.scheduleAtFixedRate(new TimerTask(){ public void run() {counter++;}}, 0, 1000L);

On stopping the service, it obviously has to do the reverse actions: stop the counter, remove the status bar notification and then toast a message to the end user saying the service has stopped! So here it is:

      Toast.makeText(this, "Service destroyed at " + time.getTime() + "; counter is at: " + counter, Toast.LENGTH_LONG).show();

This is the code in the onDestroy() method in the NotifyService class.

With this the service is ready. But we need to be able to start this service and stop it. This I have done through a ServiceLauncher class just as I had done in the previous part of this series. You can get the complete code here.


  1. Great! Just what I needed :) Normal Android documentation is overwhelming and confuses the daylight out of one whilst this did the trick.

  2. Thanks a lot man! This is GOLD!!

  3. So far EXCELLENT... this tutorial has guided me through the path I need to complete my app. Lets see what the next tutorial has to offer.

    Thank you very much Sai.

  4. I am a student and making an app in which i require to call an activity from my service. Currently i m getting "stopped unexpectedly". Is it possible to do and if yes how?

  5. Hi Geeta,
    Your blog is really useful for android beginners, I am developing a reminder app, I wanted to know how can I schedule alarm/notification when an event date matches the system date.

    Please guide me how can I go with such apps.
    Thanks a lot Sai Geeta

  6. Thanks for the post. Its been very informative

  7. hi Sai Geetha, your tutorial is very usefull..but I think the code would be easier to imagine if you put the screenshot of it. :)

  8. This comment has been removed by the author.

  9. Hi Geetha garu, By the way I'm Venkat who is new to android and working on an android project. Your examples help me a lot and I like the way of you revealing us new stuff. Thanks a ton

  10. I know u wont answer for these silly questions.Eventhough i want to ask this,How to create a customized service in the android framework level.

    Android Mobile Developer.

  11. Thanks !!!!!!!!!!!!
    YOU are KING of android app!!!!!

  12. Ur's post helped a lot, U r simply a sweetheart..... :)

  13. Very good post...But I want to call an activity from my service. is it possible?

  14. few more post for bluetooth,maps,social networks,handlers,camera apis will help new comers

  15. The most simplest post on Notifications in Services. Thanks, and keep on posting :-)

  16. Outstanding post ....

  17. Hi Geetha ,

    Nice article , help a lot for all . Being a basic android developer i stuck in the notification management. I done every thing in that management.

    I created a notification , and set it in the notification bar. My simple requirement is , I need to call a method from the activity when ever i click the notification.

    Can you please help , thanks a lot in advance.

    Even any workaround regarding in this , are welcomed.


  18. any updates on this please?????????

  19. thank you for commenting the code :)

  20. this is just what I was looking for, simple, has everything you need to get started with services, very clear, thanks a lot for sharing with us Geetha!

  21. this is very helpful. i have one doubt about notification, can i set time interval in android local notification. Is it possible? if i set also after force stop it will come. For example i set notification with 1 hour time interval like 5,6,7,8 in between i force stop the application and start my application. Now that remaining notification will come or not? can u clear my doubt? pls..

  22. I like your articles but most like is your 'NOTE' on top of the page.

  23. Android here is how i flashed this rom: 1st i Download apk reverted back to official 2.1 rom, 2nd Apllication iphone flashed to cm 7 stable.3rd updated to Apk Android MyGingerB Gaia 1.3,then lastly,updated this Gaia 2. It took so long to reboot at first,but after a couple of charging,the booting takes forever!hope this issue will be fixed Apk Androidin the next release.

  24. Now that we have worked with services and notifications separately, we are all ready to use the two together to make a real service that is notified to the user through a status bar update that the service is running. The status bar removes the notification as soon as the service is stopped.
    check this link

    1. Hello ,

      Can any one help how can i send the notification from the server(C#) which has to be display to the devices.