Frage

I have written a small application that's intended to run as a service. A few times the OS has decided to shutdown the service when freeing memory, a behavior I'm well aware I cannot stop. The documentation states that to avoid this happening you can use a foreground service by invoking startForeground and passing a notification to this method.

I have written code to show the notification, and everything appears to be working okay. There is one particular behavior I'm not too keen on however, when I press the 'power' button on my device so the screen is switched off the two 'notification' lights at the bottom of my Samsung Galaxy S 1 light up. Usually this indicates that there's something 'new' to take a look at on the phone - such as a SMS or a missed phone call. I don't think it makes much sense that these light up when the only notification available is on to say there's an on-going service.

I understand I cannot have a foreground service without a notification.

I understand that you cannot cancel a foreground service without it not being a foreground service any more.

However, is there any way to stop the lights at the bottom of this Galaxsy S1 lighting up as though there's new important information available?

Edit: Here's the code I'm using:

Intent intentForeground = new Intent(this, MainService.class)
        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intentForeground, 0);      
    Notification notification;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(R.drawable.ic_notification)
        .setTicker("Service started...")
        .setContentTitle("Vantage phone client")
        .setContentText("Service running")
        .setContentIntent(pendIntent)
        .setDefaults(Notification.DEFAULT_ALL)
        .setAutoCancel(true)
        .setOnlyAlertOnce(true)
        .setOngoing(true);

    notification = builder.build();
    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

    startForeground(1, notification);
War es hilfreich?

Lösung

You are using DEFAULT_ALL for the defaults, which will also give you default lights, and apparently on your device, default lights including lighting them up. I would remove setDefaults() or limit it to be ones other than the lights (Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE).

Andere Tipps

My code was inside onStartCommand which was invoked more than once, this is perfectly normal.

Other parts of my application were calling startService to make sure the service was running.

I have since moved the code for building the notification and invoking startForeground to onCreate where it should only get called once.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top