android - Service creating multiple notifications (loop) -
my service creating multiple notifications. can't find why, i'm using log messages see if there's loop somewhere. log cat full of notifications output ended truncated.
this looks like: http://i.imgur.com/hurhguf.png
public int onstartcommand(intent intent, int flags, int startid) { if (intent != null) { if (intent.hascategory("connection")) { connect(); } } return start_sticky; } public void connect() { thread thread = new thread() { @override public void run() { try { socket socket = new socket(server_ip, 5000); socket.setsotimeout(5000); connected = true; outputstream out = socket.getoutputstream(); printwriter output = new printwriter(out); bufferedreader input = new bufferedreader(new inputstreamreader(socket.getinputstream())); string event = input.readline(); while (event != null) { if (event.substring(0, 3).equals("msg")) { log.i("service", "notification"); notificationcompat.builder mbuilder = new notificationcompat.builder(localservice.this) .setsmallicon(r.drawable.ic_launcher) .setcontenttitle(event.substring(4, event.indexof("::"))) .setcontenttext(event.substring(event.indexof("::") + 2)); intent resultintent = new intent(localservice.this, resultactivity.class); taskstackbuilder stackbuilder = taskstackbuilder.create(localservice.this); stackbuilder.addparentstack(resultactivity.class); stackbuilder.addnextintent(resultintent); pendingintent resultpendingintent = stackbuilder.getpendingintent( 0, pendingintent.flag_update_current ); mbuilder.setcontentintent(resultpendingintent); notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service); // mid allows update notification later on. mnotificationmanager.notify(mid, mbuilder.build()); mid = mid + 1; } } } catch (unknownhostexception e) { e.printstacktrace(); } catch(java.net.connectexception e) { e.printstacktrace(); } catch (sockettimeoutexception e) { e.printstacktrace(); } catch(ioexception e){ e.printstacktrace(); } } }; thread.start(); }
}
a single message sent server, checked.
in code, should make string event = input.readline();
in while
loop. code should this:
while (event != null) { if (event.substring(0, 3).equals("msg")) { //your normal code. } event = input.readline(); }
Comments
Post a Comment