Android Automatically Update Notification Using Timer -
i want activity refresh display notification based on refreshed values every 5 minutes. got refresh button , it's working fine. when try activate refresh button before notification appears in timer. app crashes.
here codes
//oncreate
if(ordercounter>0) //ordercounter updated value { mytimertask mytask = new mytimertask(); timer mytimer = new timer(); mytimer.schedule(mytask, 1, 300000); }
//timer
class mytimertask extends timertask { public void run() { refresh.performclick(); // line cause of error pls generatenotification(getapplicationcontext(), "you have "+ ordercounter+ " pending orders!"); } } private void generatenotification(context context, string message) { int icon = r.drawable.ic_launcher; long when = system.currenttimemillis(); string appname = context.getresources().getstring(r.string.app_name); notificationmanager notificationmanager = (notificationmanager) context .getsystemservice(context.notification_service); notification notification; pendingintent contentintent = pendingintent.getactivity(context, 0, new intent(context, admin.class), 0); notificationcompat.builder builder = new notificationcompat.builder( context); notification = builder.setcontentintent(contentintent) .setsmallicon(icon).setticker(appname).setwhen(0) .setautocancel(true).setcontenttitle(appname) .setcontenttext(message).build(); notificationmanager.notify((int) when, notification); }
// refresh button
refresh.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = getintent(); intent.addflags(intent.flag_activity_no_animation); finish(); startactivity(intent); } });
i think should use alarmmanager , set activated every 5 minutes:
intent intent = new intent(context, alarmreceiver.class); alarmintent = pendingintent.getbroadcast(context, 0, intent, 0); alarmmgr.setinexactrepeating(alarmmanager.elapsed_realtime_wakeup, millisecondstofirstactivation, millisecondsforrepeatingschedule, alarmintent);
the create broadcast receiver going display notification
public class myalarmreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { displaynotification(); } }
you can find more info here: http://developer.android.com/training/scheduling/alarms.html
Comments
Post a Comment