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();
showNotification();
incrementCounter();
Here is the
showNotification() Method:
private void showNotification()
{
CharSequence text = getText(R.string.service_started);
Notification notification = new Notification(R.drawable.android, 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:
shutdownCounter();
nm.cancel(R.string.service_started);
Toast.makeText(this, "Service
destroyed at " + time.getTime() + "; counter is at: " + counter, Toast.LENGTH_LONG).show();
counter=null;
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.
Great! Just what I needed :) Normal Android documentation is overwhelming and confuses the daylight out of one whilst this did the trick.
ReplyDeleteThanks a lot man! This is GOLD!!
ReplyDeleteSo 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.
ReplyDeleteThank you very much Sai.
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?
ReplyDeleteHi Geeta,
ReplyDeleteYour 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
Ravali
Thanks for the post. Its been very informative
ReplyDeletehi Sai Geetha, your tutorial is very usefull..but I think the code would be easier to imagine if you put the screenshot of it. :)
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi 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
ReplyDeleteI 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.
ReplyDeleteNarayanan
Android Mobile Developer.
Thanks !!!!!!!!!!!!
ReplyDeleteYOU are KING of android app!!!!!
Ur's post helped a lot, U r simply a sweetheart..... :)
ReplyDeleteThanks for sharing =)
ReplyDeleteVery good post...But I want to call an activity from my service. is it possible?
ReplyDeletefew more post for bluetooth,maps,social networks,handlers,camera apis will help new comers
ReplyDeleteThe most simplest post on Notifications in Services. Thanks, and keep on posting :-)
ReplyDeleteOutstanding post ....
ReplyDeleteHi Geetha ,
ReplyDeleteNice 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.
Thanks,
Malai...
any updates on this please?????????
ReplyDeletethank you for commenting the code :)
ReplyDeletethis 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!
ReplyDeletethis 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..
ReplyDeleteI like your articles but most like is your 'NOTE' on top of the page.
ReplyDeleteAndroid 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.
ReplyDeleteNow 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.
ReplyDeletecheck this link
Hello ,
DeleteCan any one help how can i send the notification from the server(C#) which has to be display to the devices.
nic touch
ReplyDelete