gpt4 book ai didi

java - 只有一个 Android Service 实例

转载 作者:行者123 更新时间:2023-12-01 21:49:15 24 4
gpt4 key购买 nike

我有一个在我的 View 模型初始化中创建的 Service()

val myClass = MyClass()
val serviceIntent = Intent(application, myClass::class.java)

我有一个类的实例,因此我可以更新值等。

当我在 init 中启动服务时,我调用

context.stopService(serviceIntent)
context.startService(serviceIntent)

在我的 Service() 的 onDestroy 中,我调用

stopForeground(true)
stopSelf()

但是,代码被执行多次,当我调试时,我有两个服务类实例。 stopService() 不起作用吗?

最佳答案

问题是创建了我的 View 模型的多个实例。因此该服务被创建了多次

关于java - 只有一个 Android Service 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58768748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com