Android 取消后臺運行限制

首先我這邊遇到的問題是插電后workmanager(間隔15分鐘)也不運行,后來發(fā)現(xiàn)是APP被設為限制后臺運行或者Autorun被關掉了

1. 搜索發(fā)現(xiàn)下面這個方法可以判斷當前是否限制后臺運行:!!API level 28
isBackgroundRestricted Added in API level 28
當處于這個模式下捌显,即便應用充電也不行欢摄,除非前臺在運行

Query whether the user has enabled background restrictions for this app.
The user may chose to do this, if they see that an app is consuming an unreasonable amount of battery while in the background.
If true, any work that the app tries to do will be aggressively restricted while it is in the background. At a minimum, jobs and alarms will not execute and foreground services cannot 【be started 】unless an app activity is in the foreground.

**Note that these restrictions stay in effect even when the device is charging.**

| Returns |
| `boolean` | true if user has enforced background restrictions for this app, false otherwise. |

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).

所以可以文字提示并引導到自己應用的設置頁面
Intent intent=new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,  Uri.fromParts("package", getPackageName(), null))
startActivity(intent);

3. 嘗試搜索后的另外一條路: 忽略電源管理

首先要說一下由于Android 系統(tǒng)UI的定制化嚴重廊移,所以很多設備不能很好地匹配UI,或者
設置成功后無法取消舵鳞,但三星的設備表現(xiàn)還是中規(guī)中矩的震檩。

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:

  • The user explicitly launches the app.
  • The app has a process currently in the foreground (either as an activity or foreground service, or in use by another activity or foreground service).
  • The app generates a notification that users see on the lock screen or in the notification tray.
  • The app is an active device admin app (for example, a device policy controller). Although they generally run in the background, device admin apps never enter App Standby because they must remain available to receive policy from a server at any time.

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String packageName = context.getPackageName();
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            Intent intent = new Intent();
            if (!pm.isIgnoringBatteryOptimizations(packageName)) {
              
                intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
                intent.setData(Uri.parse("package:" + packageName));
              
            }else{
                intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); //BATTERY_SAVER_SETTINGS
            }
            context.startActivity(intent);
        }

Also, you need to add the following permission in manifest

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市系任,隨后出現(xiàn)的幾起案子恳蹲,更是在濱河造成了極大的恐慌,老刑警劉巖俩滥,帶你破解...
    沈念sama閱讀 206,311評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嘉蕾,死亡現(xiàn)場離奇詭異,居然都是意外死亡霜旧,警方通過查閱死者的電腦和手機错忱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來挂据,“玉大人以清,你說我怎么就攤上這事∑樘樱” “怎么了掷倔?”我有些...
    開封第一講書人閱讀 152,671評論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長个绍。 經(jīng)常有香客問我勒葱,道長,這世上最難降的妖魔是什么巴柿? 我笑而不...
    開封第一講書人閱讀 55,252評論 1 279
  • 正文 為了忘掉前任凛虽,我火速辦了婚禮,結果婚禮上广恢,老公的妹妹穿的比我還像新娘凯旋。我一直安慰自己,他們只是感情好钉迷,可當我...
    茶點故事閱讀 64,253評論 5 371
  • 文/花漫 我一把揭開白布至非。 她就那樣靜靜地躺著,像睡著了一般糠聪。 火紅的嫁衣襯著肌膚如雪睡蟋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,031評論 1 285
  • 那天枷颊,我揣著相機與錄音,去河邊找鬼。 笑死夭苗,一個胖子當著我的面吹牛信卡,可吹牛的內容都是我干的。 我是一名探鬼主播题造,決...
    沈念sama閱讀 38,340評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼傍菇,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了界赔?” 一聲冷哼從身側響起丢习,我...
    開封第一講書人閱讀 36,973評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎淮悼,沒想到半個月后咐低,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,466評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡袜腥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 35,937評論 2 323
  • 正文 我和宋清朗相戀三年见擦,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片羹令。...
    茶點故事閱讀 38,039評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡鲤屡,死狀恐怖,靈堂內的尸體忽然破棺而出福侈,到底是詐尸還是另有隱情酒来,我是刑警寧澤,帶...
    沈念sama閱讀 33,701評論 4 323
  • 正文 年R本政府宣布肪凛,位于F島的核電站堰汉,受9級特大地震影響,放射性物質發(fā)生泄漏显拜。R本人自食惡果不足惜衡奥,卻給世界環(huán)境...
    茶點故事閱讀 39,254評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望远荠。 院中可真熱鬧矮固,春花似錦、人聲如沸譬淳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽邻梆。三九已至守伸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間浦妄,已是汗流浹背尼摹。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評論 1 262
  • 我被黑心中介騙來泰國打工见芹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蠢涝。 一個月前我還...
    沈念sama閱讀 45,497評論 2 354
  • 正文 我出身青樓玄呛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親和二。 傳聞我的和親對象是個殘疾皇子徘铝,可洞房花燭夜當晚...
    茶點故事閱讀 42,786評論 2 345

推薦閱讀更多精彩內容