java - ANDROID: inside Service class, executing a method for Toast (or Status Bar notification) from scheduled TimerTask -
i trying execute {public void} method in service, scheduled timertask periodically executing.
this timertask periodically checks condition. if it's true, calls method via {classname}.{methodname};
however, java requires, method needs {pubic static} method, if want use {classname} {.dot}
the problem method notification using toast(android pop-up notification) , status bar use these notifications, 1 must use
context context = getapplicationcontext();
but work, method must not have {static} modifier , resides in service class.
so, basically, want background service evaluate condition scheduled timertask, , execute method in service class.
can me what's the right way use service, invoking method when condition satisfied while looping evaluation?
here lines of codes:
the timertask class (watchclipboard.java) :
public class watchclipboard extends timertask { //declaration private static getdefinition getdefinition = new getdefinition(); @override public void run() { if (wordup.clipboard.hastext()) { wordup.newcopied = wordup.clipboard.gettext().tostring().trim().tolowercase(); if (!(wordup.currentcopied.equals(wordup.newcopied))) { wordup.currentcopied = wordup.newcopied; log.v(wordup.tag, wordup.currentcopied); getdefinition.apicall_wordnik(); fetchservice.instantnotification(); //it requires method have {static} modifier, if want call in way. } } } }
and service class (fetchservice.java) : if change modifier static, {context} related problems occur
public class fetchservice extends service { public static final string tag = "wordup"; //for logcat filtering //declaration private static timer runningtimer; private static final boolean this_is_daemon = true; private static watchclipboard watchclipboard; private static final long delay = 0; private static final long period = 100; @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } @override public void oncreate() { log.v(wordup.tag, "fetchservice.oncreate()"); super.oncreate(); //testing service running watchclipboard = new watchclipboard(); runningtimer = new timer("runningtimer", this_is_daemon); runningtimer.schedule(watchclipboard, delay, period); } @override public void ondestroy() { super.ondestroy(); runningtimer.cancel(); stopself(); log.v(wordup.tag, "fetchservice.oncreate().stopself()"); } public void instantnotification() { //if change modifier static, {context} related problems occur context context = getapplicationcontext(); // application context //use toast notification: need accept user interaction, , change duration of show toast toast = toast.maketext(context, wordup.newcopied+": "+wordup.newdefinition, toast.length_long); toast.show(); //use status notification: need automatically expand show lines of definitions notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service); int icon = r.drawable.icon; // icon resources charsequence tickertext = wordup.newcopied; // ticker-text long when = system.currenttimemillis(); // notification time charsequence contenttitle = wordup.newcopied; //expanded message title charsequence contenttext = wordup.newdefinition; //expanded message text intent notificationintent = new intent(this, wordup.class); pendingintent contentintent = pendingintent.getactivity(this, 0, notificationintent, 0); // next 2 lines initialize notification, using configurations above notification notification = new notification(icon, tickertext, when); notification.setlatesteventinfo(context, contenttitle, contenttext, contentintent); mnotificationmanager.notify(wordup.wordup_status, notification); } }
found simple solution. trivial issue after all.
declaring troubled data members {content} , {intent} outside of method , modify static solves problem
can't understand why didn't come simple solution.
Comments
Post a Comment