【问题标题】:Context.startForegroundService() did not then call Service.startForeground() In BroadcastReceiverContext.startForegroundService() 没有在 BroadcastReceiver 中调用 Service.startForeground()
【发布时间】:2019-05-08 14:43:48
【问题描述】:

我需要你的帮助来解决这个问题:

Context.startForegroundService() did not then call Service.startForeground()

我正在使用 BroadcastReceiver 来启动通知服务:

@Override
public void onReceive(Context context, Intent intent) {
    WakeLock.acquire(context);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ContextCompat.startForegroundService(context, new Intent(context, StartNotificationService.class).putExtras(intent));
    } else {
        context.startService(new Intent(context, StartNotificationService.class).putExtras(intent));
    }
}

请问你有什么想法吗?我该如何解决这个问题? 谢谢你

【问题讨论】:

  • 您必须在前台服务中调用startForeground。这是添加的,我相信奥利奥(不太确定)。你有一个时间窗口来做这件事。如果您没有在这段时间内调用它,Android 会抛出该异常。

标签: android android-notifications


【解决方案1】:

你的Service在启动时必须调用startForeground(),否则系统将不允许它运行。

【讨论】:

    猜你喜欢
    • 2017-11-09
    • 2019-01-09
    • 2018-03-29
    • 1970-01-01
    • 2018-03-04
    • 2019-09-17
    • 1970-01-01
    • 2021-05-31
    相关资源
    最近更新 更多