【问题标题】:How to use startForeground with NotificationCompat.Builder?如何将 startForeground 与 NotificationCompat.Builder 一起使用?
【发布时间】:2017-01-25 12:12:55
【问题描述】:
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(description.getTitle())
                .setAutoCancel(false);
    NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notificationCompat=builder.build();
    notificationManager.notify(MY_ID,notificationCompat);
    startForeground(MY_ID,notificationCompat);

使用上面的代码在后台运行服务。但是当活动关闭时,服务调用 onDestroy

【问题讨论】:

  • “不工作”?什么不工作?
  • 当显示关闭服务调用 onDestroy

标签: android service android-notifications foreground-service


【解决方案1】:

您可以从IntentService 扩展您的服务,因此它使用单独的线程并设置您的通知为真

builder.setOngoing(true)

此用户无法手动删除通知后,支持工作状态的服务。 当活动关闭时,您的服务将继续工作。

【讨论】:

  • 它是一个媒体播放器服务,在后台播放
  • 如果我理解正确,您的意思是您的服务不能从 IntentService 扩展,因为媒体播放器服务是从 Service (developer.android.com/guide/topics/media/…) 扩展的。因此,请尝试将 setOngoing(true) 设置为您的通知,而不是从 IntentService 扩展。
猜你喜欢
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 2013-12-09
  • 1970-01-01
相关资源
最近更新 更多