【问题标题】:Android Foreground Service - Notification that doesn't show Samsung Galaxy device lightsAndroid 前台服务 - 不显示三星 Galaxy 设备指示灯的通知
【发布时间】:2013-07-16 12:41:12
【问题描述】:

我编写了一个旨在作为服务运行的小型应用程序。有几次操作系统决定在释放内存时关闭服务,我很清楚我无法停止这种行为。文档指出,为避免这种情况发生,您可以通过调用 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);

【问题讨论】:

  • 您可能会考虑在配置 Notification 的位置设置您传递给 startForeground() 的代码。
  • 我可能会,我做到了。见编辑。

标签: android service notifications foreground light


【解决方案1】:

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

【讨论】:

  • 感谢您的及时回复。您的回答是正确的,尽管我的应用程序中发生这种情况的原因在我准备发表的一篇文章中有详细说明...
【解决方案2】:

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

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

此后,我已将用于构建通知和调用 startForeground 的代码移至 onCreate,它应该只被调用一次。

【讨论】:

    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多