我们有一个几乎永远使用前台服务运行的应用程序,同时在系统托盘上使用通知,这是正常的初始化。应用程序只是依赖于这个服务。在我们测试的每一台设备上,即使任务被删除,也会继续运行,但在小米设备上,在从最近的设备上滑动之后,它突然停止,然后根据ActivityManager决定重新打开服务的方式再次启动。我们正在从小米设备(小米MI9 )那里获得日志,比如:
Scheduling the restart of the crashed service: com.example.myapp/.MyService in 1000ms
这不应该发生,但它确实发生了。每次我们打开应用程序,关闭它的最近,1000ms部分不断增加到4000ms, 16000ms, 64000ms等等。我不认为它有一个限制,64秒已经太长,一个前台服务重新启动,这是至关重要的应用程序。因此,我正在寻找方法来添加我们的应用程序作为一个例外或什么,但我发现的唯一东西是:https://dontkillmyapp.com/xiaomi
如果应用程序是在最近的屏幕上用X按钮关闭的,那就更糟糕了,因为我注意到设备杀死了所有的服务,并安排它们在10秒钟内重新启动。我认为我们的计划在3个小时后开始,这破坏了应用程序的用途。
我们目前使用的解决方案是警告用户注意这个问题并重定向到这个链接,以便将我们的应用程序添加到异常中,启用Autostart等等。但是,我们知道几乎没有人会这样做,所以我们正在寻找一种可以通过编程实现的解决方案。
一小部分代码演示了我们如何注册要显示的服务以及如何启动它。(演示比原始示例简单,但描述了主要逻辑。)
清单部分:
<service android:name=".MyService"
android:stopWithTask="false" />启动服务部分:
// Starts the service as foreground.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
context.startForegroundService(new Intent(context, MyService.class));
else
context.startService(new Intent(context, MyService.class));张贴通知部分:
// Post the notification on both onCreate and
// onStartCommand so we can only hope that
// the app won't throw the unavoidable exception
// which occurs 5 seconds after calling
// Context.startForegroundService().
@Override
public void onCreate()
{
super.onCreate();
// Handles how the notification
// is shown, content is not important.
// Calls startForeground inside.
showNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
showNotification();
// Some other service code that is irrelevant
// Return START_STICKY so we can ensure that if the
// service dies for some reason, it should start back.
return START_STICKY;
}我认为一切都是正确的,因为这只发生在小米设备上,但我们无法找到一个保持这一服务存活的解决方案。还有其他人也经历过同样的事情吗?我们该怎么做这样我们的服务才不会死?谢谢你的帮助。
发布于 2019-11-19 07:50:54
去设置->权限->自动启动,然后选择你的应用程序
https://stackoverflow.com/questions/57976116
复制相似问题