Android local notifications are slightly different, divided into immediate trigger and delay trigger
1. Instant notification
Android default notification for immediate triggering
Intent Intent =NewIntent (Intent.action_view); Intent.addcategory (Intent.category_launcher); Intent.setclass (_gameactivity, _gameactivity.getclass ()); Intent.setflags (Intent.flag_activity_new_task|intent.flag_activity_reset_task_if_needed); Pendingintent content= pendingintent.getactivity (context, 0, intent, 0); FinalNotificationmanager notimgr =(Notificationmanager) Context.getsystemservice (Context.notification_service); FinalNotification Noti =NewNotification (R.drawable.icon, Notiresult.getpayload (), System.currenttimemillis ()); String app_name=_gameactivity.getresources (). getString (R.string.app_name); Noti.setlatesteventinfo (context, App_name, Notiresult.getpayload (), content); Notimgr.notify (Atomicinteger.incrementandget (), noti);
2. Delay Trigger
Want to let a certain time after the trigger notice, in fact, is to use the system Alarmmanager to achieve.
Specific Alarmmanager explanation reference
http://yuanzhifei89.iteye.com/blog/1131523
http://blog.csdn.net/ryantang03/article/details/9317499
Add a alarm_service
/** Name: notification name, as notification ID using * content: Notification content * Time: Rewind (seconds) **/ Public Static voidAddlocalnotication (string name, string content,intTime ) {Calendar cal=calendar.getinstance (); Cal.settimeinmillis (System.currenttimemillis ()); Cal.add (Calendar.second, (int) time); Activity Activity=_gameactivity; Intent Intent=NewIntent (activity, Notificationreceiver.class); Intent.setclass (activity, Notificationreceiver.class); Intent.setdata (Uri.parse (name));
Intent.putextra ("msg", "Play_hskay");
Intent.putextra ("content", content);
= Pendingintent.getbroadcast (activity, 0, intent, pendingintent.flag_update_current); = (Alarmmanager) Activity.getsystemservice (context.alarm_service); Am.set (Alarmmanager.rtc_wakeup, Cal.gettimeinmillis (), pi); }
Add a system notification class to receive system polling notifications when the countdown is reached
Importjava.util.List;ImportJava.util.concurrent.atomic.AtomicInteger;ImportAndroid.app.ActivityManager;ImportAndroid.app.ActivityManager.RunningAppProcessInfo;ImportAndroid.app.ActivityManager.RunningTaskInfo;Importandroid.app.Notification;ImportAndroid.app.NotificationManager;Importandroid.app.PendingIntent;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.content.pm.PackageInfo;ImportAndroid.content.pm.ResolveInfo;Importandroid.content.pm.PackageManager.NameNotFoundException;ImportAndroid.util.Log;ImportAndroid.widget.Toast; Public classNotificationreceiverextendsbroadcastreceiver{ PublicNotificationreceiver () {} @Override Public voidOnReceive (Context context, Intent itent) {//Toast.maketext (Context, "the alarm rang, can do something ~ ~", Toast.length_long). Show (); String msg = Itent.getstringextra ("msg")); String content= Itent.getstringextra ("Content"); //determine if the app is running in the foreground if(istopactivity (context)) {return; } //Push a notificationshownotification (context,content); return; } Public voidshownotification (context context, String msg) {Notificationmanager Barmanager=(Notificationmanager) Context.getsystemservice (Context.notification_service); Notification Notice=NewNotification (R.drawable.icon,msg,system.currenttimemillis ()); Notice.flags=Notification.flag_auto_cancel; Intent appintent=NewIntent (Intent.action_main); Appintent.addcategory (Intent.category_launcher); Appintent.setcomponent (NewComponentName (Context.getpackagename (), context.getpackagename () + "." + "Splash")); //Setting the startup modeAppintent.setflags (intent.flag_activity_new_task|intent.flag_activity_reset_task_if_needed); Pendingintent contentintent= pendingintent.getactivity (Context, 0, Appintent, pendingintent.flag_update_current); String app_name=context.getresources (). getString (R.string.app_name); Notice.setlatesteventinfo (context, App_name, MSG, contentintent); Atomicinteger Atomicinteger=NewAtomicinteger (1); Barmanager.notify (0, notice); } protected Static Booleanistopactivity (Context activity) {String PackageName=Activity.getpackagename (); Activitymanager Activitymanager=(Activitymanager) Activity.getsystemservice (Context.activity_service); List<RunningTaskInfo> tasksinfo = activitymanager.getrunningtasks (1); if(Tasksinfo.size () > 0) {LOG.D ("Test", "---------------Package name-----------" +tasksinfo.get (0). Topactivity.getpackagename ()); //the application is on the top level of the stack if(Packagename.equals (tasksinfo.get (0). Topactivity.getpackagename ())) {return true; } } return false; }}
<receiverAndroid:name=". Notificationreceiver "android:permission= "Com.google.android.c2dm.permission.SEND"> <Intent-filter> <ActionAndroid:name= "Play_hskay" /> </Intent-filter> </receiver>
Android Local timer notification