Android前台服务 - 未显示三星Galaxy设备指示灯的通知

时间:2013-07-17 19:31:07

标签: android service notifications foreground light

我编写了一个旨在作为服务运行的小应用程序。有几次操作系统决定在释放内存时关闭服务,这种行为我很清楚我无法停止。该文档指出,为了避免这种情况发生,您可以通过调用startForeground并将通知传递给此方法来使用前台服务。

我已经编写了代码来显示通知,而且一切似乎都正常。然而,当我按下设备上的“电源”按钮以便屏幕关闭时,我的三星Galaxy S 1底部的两个“通知”指示灯亮起,有一个特别的行为我并不太热衷。通常这表明在手机上有一些“新”的东西 - 例如短信或错过的电话。我认为,当唯一可用的通知表明有正在进行的服务时,这些点亮是不明智的。

我知道如果没有通知,我就无法拥有前台服务。

据我所知,如果前台服务不再是前台服务,就无法取消它。

但是,有没有办法阻止Galaxsy S1灯光底部的灯光亮起,好像有新的重要信息可供使用?

编辑:这是我正在使用的代码:

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);

2 个答案:

答案 0 :(得分:1)

您使用DEFAULT_ALL作为默认设置,这也会为您提供默认灯光,并且显然在您的设备上,默认灯光包括点亮它们。我会删除setDefaults()或将其限制为灯光以外的其他广告(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)。

答案 1 :(得分:0)

我的代码在onStartCommand里面被多次调用,这是完全正常的。

我的应用程序的其他部分是调用startService来确保服务正在运行。

我已经移动了用于构建通知的代码并将startForeground调用到onCreate,它只能被调用一次。