From c4351ec6871946f41b37936923a9b7b343691340 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Wed, 27 Mar 2013 11:03:51 +0200 Subject: [PATCH] Fix the service notification The service notification is no longer updated on each service call, and the InboxStyle finally works. --- src/ro/ieval/fonbot/FonBotMainService.java | 26 +++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ro/ieval/fonbot/FonBotMainService.java b/src/ro/ieval/fonbot/FonBotMainService.java index 47fdafc..dba5389 100644 --- a/src/ro/ieval/fonbot/FonBotMainService.java +++ b/src/ro/ieval/fonbot/FonBotMainService.java @@ -104,6 +104,9 @@ public final class FonBotMainService extends Service { */ private final Set ongoing=EnumSet.noneOf(OngoingEvent.class); + /** true if running in foreground, false otherwise */ + private boolean isForeground = false; + /** * Get the set of ongoing events. * @@ -132,13 +135,16 @@ public final class FonBotMainService extends Service { @Override public int onStartCommand(final @Nullable Intent intent, final int flags, final int startId) { + boolean updateNotification = false; if(intent!=null && intent.getAction()==ACTION_PUT_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){ ongoing.add(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]); LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE)); + updateNotification=true; } if(intent!=null && intent.getAction()==ACTION_DELETE_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){ ongoing.remove(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]); LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE)); + updateNotification=true; } if(intent!=null && intent.getAction()==ACTION_TRIGGER_POLL) @@ -148,7 +154,9 @@ public final class FonBotMainService extends Service { if(!runForeground) stopForeground(true); - if(runForeground||!ongoing.isEmpty()){ + final NotificationManager man=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); + final boolean shouldNotify=runForeground||!ongoing.isEmpty(); + if(shouldNotify && (updateNotification || runForeground != isForeground)){ final Intent mainIntent=new Intent(this, FonBotMainActivity.class); mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); final NotificationCompat.Builder builder=new NotificationCompat.Builder(this). @@ -159,26 +167,24 @@ public final class FonBotMainService extends Service { setContentIntent(PendingIntent.getActivity(this, 0, mainIntent, 0)). setOngoing(true); - final Notification notification; - if(ongoing.isEmpty()) - notification=builder.build(); - else { + if(!ongoing.isEmpty()) { final NotificationCompat.InboxStyle inboxBuilder=new NotificationCompat.InboxStyle(builder); for(final OngoingEvent event : ongoing) inboxBuilder.addLine(getString(event.resource)); - notification=inboxBuilder.build(); } + final Notification notification=builder.build(); if(runForeground) - startForeground(1337, builder.build()); - else{ - final NotificationManager man=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); + startForeground(1337, notification); + else man.notify(1337, notification); - } } + if(!shouldNotify) + man.cancel(1337); + isForeground=runForeground; return START_STICKY; } } -- 2.30.2