小米设备正在停止前台服务

问题描述 投票:0回答:1

我们有一个使用前景服务几乎永远运行的应用程序,同时在系统托盘上使用通知,这是正常的初始化。 [该应用程序仅依赖于此服务。在我们测试的每台设备上,即使删除了任务,该服务仍在运行,但是在小米设备上,从最新消息中刷过后,它突然停止,然后根据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小时后启动,这破坏了该应用程序的用途。

我们正在使用的当前解决方案是警告用户该问题并重定向到此链接,以便将我们的应用添加到例外中,并启用自动启动等。但是,我们知道几乎没有人会这样做,因此我们正在寻找可以以编程方式实现的解决方案。

少量代码演示了我们如何注册服务以实现清单以及如何启动它。 (该示例比原始示例要简单,但描述了主要逻辑。)

清单部分:

<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;
}

我认为一切都正确完成,因为这仅在小米设备上发生,但是我们找不到有关保持此服务正常运行的解决方案。还有其他人遇到过同样的事情吗?我们应该如何进行以免我们的服务不消失?感谢您提供的所有帮助。

android android-service foreground-service xiaomi
1个回答
0
投票

转到设置->权限->自动启动,然后选择您的应用

© www.soinside.com 2019 - 2024. All rights reserved.