gpt4 book ai didi

java - 关于Android Service生命周期的一些问题

转载 作者:行者123 更新时间:2023-12-01 16:52:08 29 4
gpt4 key购买 nike

来自official doc :

The Android system will force-stop a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, then it's less likely to be killed

当一个Service确实被系统杀死时,是调用onStop()还是onDestroy()

考虑下面的代码 fragment :

public class MyService extends Service {

int count = 0;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
count++;
return START_STICKY;
}

...
}

通过在 onStartCommand() 中返回 START_STICKY,我假设 MyService 在由于内存不足而被系统杀死后将重新创建自身。每次MyService重新创建时count的值都会重置为0吗?

最佳答案

When a Service is indeed killed by the system, is onStop() or onDestroy() get called?

Service 没有可用的 onStop()Service 可以用 onDestroy() 调用,也可以不调用。

Will the value of count resetted to 0 everytime MyService recreates?

是的。

关于java - 关于Android Service生命周期的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062439/

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