【问题标题】:Activity calles onStartCommand of serviceActivity调用服务的onStartCommand
【发布时间】:2013-09-16 06:12:22
【问题描述】:

我有一个后台服务,可以推送通知。当您单击通知时,我的活动已打开,但问题是,再次调用 onStartCommand,这可能会调用另一个通知,这意味着我有一个循环。

我正在使用

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_NOT_STICKY;
}

还尝试了 Service.START_STICKY,但没有任何效果。

在我的清单中:

        <service
        android:name=".notification.NotificationService"
        android:exported="false"></service>

什么会导致 onStartCommand 被召回?

【问题讨论】:

标签: android service


【解决方案1】:

检查您的Activity 代码,听起来像是您不小心启动了Service
在某些情况下可能会发生这种情况,例如 it may be killed by the system if it is under heavy memory pressure. If this happens, the system will later try to restart the service.

还有另一种可能发生这种情况。如果您旋转您的设备,您的 Activity 将被杀死并根据 Android official docs Activity 重新启动。您的Service 将再次启动。阅读文档,节省一些时间和头痛。在此链接Android official docs Activity 上,向下滚动 2 圈,您会看到生命周期的图片。

【讨论】:

  • 是的,我在第一个应用程序启动时在我的活动中启动它。这段代码只有一次,已经检查过了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 2019-12-22
相关资源
最近更新 更多