【问题标题】:Android Foreground Service check if createdAndroid前台服务检查是否已创建
【发布时间】:2014-09-23 08:17:03
【问题描述】:

我的应用程序中有一个服务,它是一个医疗应用程序,并且该服务应该在应用程序安装的整个过程中运行而不会被杀死。 所以我尝试了几种方法,尝试使用返回START_STICKY 的常规服务,但这并没有完全按照预期重新创建服务。然后我决定尝试使用前台服务。 我正在创建它赢得这样的服务 onStartCommand

    startForeground(FOREGROUND_ID, buildForegroundNotification());
    return START_STICKY;
}

private Notification buildForegroundNotification() {
    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    b.setOngoing(true);
    b.setContentTitle("My App");
    b.setContentText("Therapy is running");
    b.setSmallIcon(R.drawable.ic_launcher);
    return (b.build());
}

所以我有两个问题: - 前台服务可以被安卓杀死吗? - 如何检查通知是否已经创建,因此它不会在每次服务启动时创建它(当 Activity 也在运行时,服务会被多次重新创建,因为它正在与它进行主动通信)

【问题讨论】:

    标签: android android-service android-notifications foreground-service


    【解决方案1】:

    第一个问题,见here

    第二个问题:我想你正在使用myNotificationManager.notify(int id, Notification notification)。 ID 字段用于标识您要显示的通知类型,因此为相同类型的通知设置相同的 ID 会将旧通知替换为新创建的通知(如果仍然显示旧通知)或简单地显示一个新的(见NotificationManager docs)。

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2021-10-26
      • 2017-10-24
      • 2022-09-23
      相关资源
      最近更新 更多