gpt4 book ai didi

android - 关闭屏幕后减慢android应用程序的自动关闭过程

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:53 25 4
gpt4 key购买 nike

我在 android 平台上创建了一个音乐应用程序,但是当我让它在屏幕关闭时运行几分钟后,应用程序关闭并且不再播放音乐。那么如何延长应用程序在屏幕关闭时的运行时间呢?

最佳答案

我假设您正在运行 Service 以在后台播放音乐。因此,当操作系统认为移动设备必须减少电池消耗时,它会在运行后的某个时间终止您的服务

现在,您可以做的就是在前台 中运行它来避免这种情况。因为那时该服务被用户标记为有用,所以除非用户自己这样做,否则它不会被杀死。

详细了解如何将服务设置为在前台运行 here in the documentation

上面的链接明确提到:

a music player that plays music from a service should be set to run in the foreground

但是请记住,只有当用户知道任务正在完成时,完成任何任务的任何服务都应该在前台运行。

You should only use a foreground service when your app needs to perform a task that is noticeable by the user even when they're not directly interacting with the app.

因此,您必须显示一条通知,提及服务正在前台运行,以完成任务,以便用户可以对此的了解。

A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

关于android - 关闭屏幕后减慢android应用程序的自动关闭过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730662/

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