forked from Github/cordova-plugin-run-in-background
New "disableBatteryOptimizations" function for Android
This commit is contained in:
@@ -21,12 +21,14 @@
|
||||
|
||||
package de.appplant.cordova.plugin.background;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.app.ActivityManager;
|
||||
import android.app.ActivityManager.AppTask;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.PowerManager;
|
||||
import android.view.View;
|
||||
@@ -43,6 +45,8 @@ import java.util.List;
|
||||
import static android.content.Context.ACTIVITY_SERVICE;
|
||||
import static android.content.Context.POWER_SERVICE;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static android.os.Build.VERSION_CODES.M;
|
||||
import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;
|
||||
import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
|
||||
import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
|
||||
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
|
||||
@@ -97,7 +101,10 @@ class BackgroundExt {
|
||||
{
|
||||
switch (action)
|
||||
{
|
||||
case "optimizations":
|
||||
case "batteryoptimizations":
|
||||
disableBatteryOptimizations();
|
||||
break;
|
||||
case "webviewoptimizations":
|
||||
disableWebViewOptimizations();
|
||||
break;
|
||||
case "background":
|
||||
@@ -179,6 +186,26 @@ class BackgroundExt {
|
||||
thread.start();
|
||||
}
|
||||
|
||||
@SuppressLint("BatteryLife")
|
||||
private void disableBatteryOptimizations()
|
||||
{
|
||||
Activity activity = cordova.getActivity();
|
||||
Intent intent = new Intent();
|
||||
String pkgName = activity.getPackageName();
|
||||
PowerManager pm = (PowerManager)getService(POWER_SERVICE);
|
||||
|
||||
if (SDK_INT < M)
|
||||
return;
|
||||
|
||||
if (pm.isIgnoringBatteryOptimizations(pkgName))
|
||||
return;
|
||||
|
||||
intent.setAction(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
|
||||
intent.setData(Uri.parse("package:" + pkgName));
|
||||
|
||||
cordova.getActivity().startActivity(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes the app from the recent tasks list.
|
||||
*/
|
||||
|
||||
@@ -104,7 +104,8 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
case "disable":
|
||||
disableMode();
|
||||
break;
|
||||
case "optimizations":
|
||||
case "webviewoptimizations":
|
||||
case "batteryoptimizations":
|
||||
case "background":
|
||||
case "foreground":
|
||||
case "tasklist":
|
||||
@@ -303,5 +304,4 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
|
||||
cordova.getActivity().runOnUiThread(() -> webView.loadUrl("javascript:" + js));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -320,5 +320,4 @@ public class ForegroundService extends Service {
|
||||
{
|
||||
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user