multithreading - Android Service running multiple times -
i making android app pulls data notifications server, have used service. service runs in background timer of 30 seconds. , every 30 seconds, service check , pull notification, problem mobile pulls 3 times more notification actual number of notification. not understand going on. here service class:
public class notificationservices extends service { int iuniqueid = (int) (system.currenttimemillis() & 0xfffffff); private final int updateintercal = 30 * 1000; private timer timer = new timer(); private static final int notificationex = 0; private notificationcompat.builder mbuilder; private pendingintent resultpendingintent; int mnotificationid = 001; private notificationmanager mnotifymgr; private intent resultintent; private string messagerecipientid; private int recipientid = 0; int counter = 0; //private boolean lock = false; @override public ibinder onbind(intent arg0) { return null; } @override public int onstartcommand(intent intent, int flags, int startid) { // let continue running until stopped. //toast.maketext(this, "service started", toast.length_long).show(); databasehandler dh = new databasehandler(notificationservices.this); try { dh.open(); recipientid = dh.getrecipientid(); dh.close(); } catch (sqlexception e) { e.printstacktrace(); } timer.scheduleatfixedrate(new timertask() { @override public void run() { // check if there updates here , notify if true log.d("service counter", counter + ""); counter++; requestqueue queue = volley.newrequestqueue(getapplicationcontext()); string url = getresources().getstring(r.string.apiurl) + "/registration/checknotification/recipientid/" + recipientid; stringrequest stringrequest = new stringrequest(request.method.get, url, new response.listener<string>() { @override public void onresponse(string response) { try { log.d("response notification", response); mnotifymgr = (notificationmanager) getsystemservice(notification_service); jsonobject jsonobject = new jsonobject(response); jsonarray jsonarray = jsonobject.getjsonarray("data"); (int = 0; < jsonarray.length(); i++) { mbuilder = new notificationcompat.builder(notificationservices.this); mbuilder.setautocancel(true);//removes notification status bar once clicked on //vibration mbuilder.setvibrate(new long[]{500, 500, 500, 500, 500}); mbuilder.setsound(ringtonemanager.getdefaulturi(ringtonemanager.type_notification)); mbuilder.setlights(color.red, 3000, 3000); mbuilder.setsmallicon(r.drawable.notification_icon) .setcontenttitle(jsonarray.getjsonobject(i).getstring("subject")) .setcontenttext(jsonarray.getjsonobject(i).getstring("messagebody")); //passing category mainactivity using bundle bundle bundle = new bundle(); bundle.putint("message_recipientid", jsonarray.getjsonobject(i).getint("message_recipientid")); bundle.putstring("category", jsonarray.getjsonobject(i).getstring("category")); bundle.putint("studentid", jsonarray.getjsonobject(i).getint("studentid")); bundle.putstring("subject", jsonarray.getjsonobject(i).getstring("subject")); bundle.putstring("message", jsonarray.getjsonobject(i).getstring("messagebody")); bundle.putstring("date", jsonarray.getjsonobject(i).getstring("datescheduled")); bundle.putboolean("fromnotification", true); //for action of notification, use pendingintent resultintent = new intent(notificationservices.this, mainactivity.class); resultintent.putextras(bundle);//passing bundle in resultintent resultintent.addflags(intent.flag_activity_single_top | intent.flag_activity_clear_top); log.d("bundlevalues", bundle.getint("studentid") + " " + bundle.getstring("message")); resultpendingintent = pendingintent.getactivity(notificationservices.this, mnotificationid, resultintent, pendingintent.flag_update_current); mbuilder.setcontentintent(resultpendingintent); //log.d("bundlevaluesafter", bundle.getint("studentid")+" "+bundle.getstring("message")); mnotifymgr.notify(mnotificationid, mbuilder.build());//sends notification mnotificationid++; //messagerecipientid = jsonarray.getjsonobject(i).getstring("message_recipientid"); //getting current date send server calendar c = calendar.getinstance(); simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string formatteddate = df.format(c.gettime()); log.d("seedate", formatteddate); //save message sqlite database databasehandler dh = new databasehandler(getapplicationcontext()); dh.open(); dh.savenotification(jsonarray.getjsonobject(i).getint("message_recipientid"), jsonarray.getjsonobject(i).getint("studentid"), jsonarray.getjsonobject(i).getstring("datescheduled"), jsonarray.getjsonobject(i).getstring("messagebody"), jsonarray.getjsonobject(i).getstring("subject"), jsonarray.getjsonobject(i).getstring("category")); dh.close(); /*//setting message recieved requestqueue queue = volley.newrequestqueue(getapplicationcontext()); //string url = "http://hsm.edu.np/hsmapp/api/registration/notificationseen/message_recipientid/" string url = getresources().getstring(r.string.apiurl) + "/registration/notificationseen/message_recipientid/" + messagerecipientid + "/date/" + formatteddate; stringrequest stringrequest = new stringrequest(request.method.get, url, new response.listener<string>() { @override public void onresponse(string response) { log.d("response", response); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.d("response", error.tostring()); } }); queue.add(stringrequest);*/ } } catch (jsonexception e1) { e1.printstacktrace(); } catch (sqlexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } } , new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.d("response", "error" + error); } } ); queue.add(stringrequest); } } , 0, updateintercal); return start_sticky; } private void stopservice() { if (timer != null) timer.cancel(); log.d("service status:", "stopped"); } @override public void ondestroy() { super.ondestroy(); if (timer != null) { timer.cancel(); } toast.maketext(this, "service destroyed", toast.length_long).show(); } } and service have started in splash activity, this:
startservice(new intent(splashscreen.this, notificationservices.class)); i learning method, can me, did wrong? have no idea problem is??
Comments
Post a Comment