gpt4 book ai didi

android - 这是即使在低内存压力下也能保持服务 Activity 的正确技巧吗?

转载 作者:行者123 更新时间:2023-11-30 03:27:17 25 4
gpt4 key购买 nike

我使用了以下代码来保持服务的 Activity :

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
keepServiceAlive(this, 2222);
return super.onStartCommand(intent, flags, startId);
}
@SuppressWarnings("deprecation")
public void keepServiceAlive(Service service, int id) {
Notification note = new Notification(0, null, System.currentTimeMillis());
note.flags |= Notification.FLAG_NO_CLEAR;
service.startForeground(id, note);
}

但有时服务仍然被杀死。您能帮我确认一下这段代码的正确性吗,或者您能提供更好的解决方案吗?

最佳答案

您不应创建格式错误的 Notification 对象。 Android 4.3 现在会在检测到此黑客攻击时向用户显示它自己的通知,并且会通知用户您的服务正在运行。如果你真的需要它在前台,给它一个合适的图标,让你的用户知道你的服务,并解释为什么它是必要的。您甚至可以考虑让他们决定是否通过 SharedPreferences 将该服务作为前台服务运行。

参见 this blog post by CommonsWare

关于android - 这是即使在低内存压力下也能保持服务 Activity 的正确技巧吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18037934/

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