Android Local timer notification

Source: Internet
Author: User

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

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.content.BroadcastReceiver;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;;;;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= "">            <Intent-filter>                <ActionAndroid:name= "Play_hskay" />            </Intent-filter>        </receiver>

Android Local timer notification

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.