Android 取消后台运行限制

 我来答
世纪网络17
2022-07-09 · TA获得超过5957个赞
知道小有建树答主
回答量:2426
采纳率:100%
帮助的人:144万
展开全部

1. 搜索发现下面这个方法可以判断当前是否限制后台运行:!!API level 28
isBackgroundRestricted Added in API level 28
当处于这个模式下,即便应用充电也不行,除非前台在运行

2. 关于打开控制APP后台限制的设置页面的搜索:

Open Background Restriction in settings
"Background Restriction" (or "Allow Background Activity" on some devices) is intended to stop ALL background activity regardless of whether your service has called setForeground()
There is no way around this setting. You cannot programmatically disable it. Your only option is to programmatically check if it's enabled using ActivityManager.isBackgroundRestricted() and display a pop-up informing your users on how to disable this setting
Google's Universal Music Player sample project on GitHub happens to work (as of the writing of this answer) only because a service bind is not released when the main Activity is paused. The sample project's service is however killed when the main Activity is garbage collected (typically 30-45 minutes depending on the device).

3. 尝试搜索后的另外一条路: 忽略电源管理

首先要说一下由于Android 系统UI的定制化严重,所以很多设备不能很好地匹配UI,或者
设置成功后无法取消,但三星的设备表现还是中规中矩的。

https://stackoverflow.com/posts/48859802/timeline

This is part of the new App Standby(应用待机) feature introduced with API 23 (Marshmallow) alongside Doze Battery Optimization aimed to optimize power and resource usage while the app is in background (App Standby) or when the device has long been in sleep (Doze).

Following is the explanation from the Android Developer's site page :

Specifically, in the App Standby mode, the system determines that an app is idle when the user is not actively using it. The system makes this determination when the user does not touch the app for a certain period of time and none of the following conditions applies:

When the user plugs the device into a power supply, the system releases apps from the standby state, allowing them to freely access the network and to execute any pending jobs and syncs. If the device is idle for long periods of time, the system allows idle apps network access around once a day.

So, this means that starting from API 23 (Marshmallow), the device may actively put your app on standby, preventing network access (say for task like sync) and limiting (or disabling) background executions. Now, for most of the apps this behavior is fine or you could easily optimize for such behavior, but for some apps out there this may cause some unexpected behavior, especially for apps that have been poorly optimized or use non-standard sync strategies or some other means of background sync/execution. So to circumvent that, the user can explicitly mark the app as non-optimized and the system will fallback and not put the app to standby, although this leads to a poorer user experience and you should not be doing this for regular apps that could be optimized.

resolvent

Also, you need to add the following permission in manifest

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式