recently, there is a project request is in the program exit, still can send a notice every day, we can think of, in fact, is the background to open a service, and then the time to send a notice.
1. First we need to use the service class.
First, the code is slowly explaining.
1 Packagecom.example.androidnotification;2 3 ImportJava.util.Timer;4 ImportJava.util.TimerTask;5 Importandroid.app.Notification;6 ImportAndroid.app.NotificationManager;7 Importandroid.app.PendingIntent;8 ImportAndroid.app.Service;9 Importandroid.content.Intent;Ten ImportAndroid.os.IBinder; One ImportAndroid.util.Log; A - Public classPushserviceextendsService { - the StaticTimer timer =NULL; - //Clear Notifications - Public Static voidcleanallnotification () { -Notificationmanager mn=(Notificationmanager) Mainactivity.getcontext (). Getsystemservice (Notification_service); + Mn.cancelall (); - if(Timer! =NULL) { + Timer.cancel (); ATimer =NULL; at } - } - - //Add notification - Public Static voidAddNotification (intdelaytime,string tickertext,string contenttitle,string contenttext) - { inIntent Intent =NewIntent (Mainactivity.getcontext (), Pushservice.class); -Intent.putextra ("Delaytime", delaytime); toIntent.putextra ("Tickertext", tickertext); +Intent.putextra ("Contenttitle", contenttitle); -Intent.putextra ("ContentText", ContentText); the Mainactivity.getcontext (). StartService (intent); * } $ Panax Notoginseng Public voidonCreate () { -LOG.E ("AddNotification", "===========create======="); the } + A @Override the Publicibinder onbind (Intent arg0) { + //TODO auto-generated Method Stub - return NULL; $ } $ - Public intOnstartcommand (FinalIntent Intent,intFlagsintStartid) { - the Longperiod = 24*60*60*1000;//24 hours a cycle - intDelay=intent.getintextra ("Delaytime", 0);Wuyi if(NULL==timer) { theTimer =NewTimer (); - } WuTimer.schedule (NewTimerTask () { - About @Override $ Public voidrun () { - //TODO auto-generated Method Stub -Notificationmanager mn= (Notificationmanager) pushservice. This. Getsystemservice (Notification_service); -Notification.builder Builder =NewNotification.builder (Pushservice. This); AIntent notificationintent =NewIntent (Pushservice. This, Mainactivity.class);//Click Jump Location +Pendingintent contentintent = pendingintent.getactivity (pushservice. This, 0,notificationintent,0); the builder.setcontentintent (contentintent); - Builder.setsmallicon (r.drawable.ic_launcher); $Builder.setticker (Intent.getstringextra ("Tickertext"));//test the notification bar title theBuilder.setcontenttext (Intent.getstringextra ("ContentText"));//drop-down notification content theBuilder.setcontenttitle (Intent.getstringextra ("Contenttitle"));//Drop- down notification bar header theBuilder.setautocancel (true); the builder.setdefaults (notification.default_all); -Notification Notification =builder.build (); inMn.notify ((int) System.currenttimemillis (), notification); the } the },delay, period); About the return Super. Onstartcommand (Intent, flags, startid); the } the + @Override - Public voidOnDestroy () { theLOG.E ("AddNotification", "===========destroy=======");Bayi Super. OnDestroy (); the } the}
Customized a class Pushservice continuation service with two classes defined for adding notifications and canceling notifications
Delaytime How long the delay executes.
Tickertext
Contenttitle the title of the notification bar
ContentText the contents of the notification bar
AddNotification (int delaytime,string tickertext,string contenttitle,string contenttext)
Clear Notifications
Cleanallnotification ()
====================================
Service startup, StartService to start the services only once OnCreate methods are executed, but each time StartService is invoked, the Onstartcommand function is executed once.
2. Registration Service class
Register this service class by adding the following information to the application field in Androidmanifest.xml.
<android:enabled= "true" android:name= ". Pushservice " android:process=" System "> </Service >
It's a little bit important here. Android:process= "System", set to System, or press the exit key to execute as follows will cause the program to crash
Android.os.Process.killProcess (Android.os.Process.myPid ()); or system.exit (0)
And the service will also be terminated, because the services are together with the main thread, the main thread is terminated, the service thread will stop, it will not be executed in the background, so we must register the service into the system
Project Source
Android Background Service timer notification