mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-26 01:04:55 +00:00
New "disableBatteryOptimizations" function for Android
This commit is contained in:
parent
9fb7257297
commit
537f7a859f
@ -75,6 +75,7 @@
|
|||||||
|
|
||||||
<config-file target="AndroidManifest.xml" parent="/manifest">
|
<config-file target="AndroidManifest.xml" parent="/manifest">
|
||||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
|
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||||
</config-file>
|
</config-file>
|
||||||
|
|
||||||
<source-file
|
<source-file
|
||||||
|
@ -21,12 +21,14 @@
|
|||||||
|
|
||||||
package de.appplant.cordova.plugin.background;
|
package de.appplant.cordova.plugin.background;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.ActivityManager;
|
import android.app.ActivityManager;
|
||||||
import android.app.ActivityManager.AppTask;
|
import android.app.ActivityManager.AppTask;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@ -43,6 +45,8 @@ import java.util.List;
|
|||||||
import static android.content.Context.ACTIVITY_SERVICE;
|
import static android.content.Context.ACTIVITY_SERVICE;
|
||||||
import static android.content.Context.POWER_SERVICE;
|
import static android.content.Context.POWER_SERVICE;
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
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_ALLOW_LOCK_WHILE_SCREEN_ON;
|
||||||
import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
|
import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
|
||||||
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
|
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
|
||||||
@ -97,7 +101,10 @@ class BackgroundExt {
|
|||||||
{
|
{
|
||||||
switch (action)
|
switch (action)
|
||||||
{
|
{
|
||||||
case "optimizations":
|
case "batteryoptimizations":
|
||||||
|
disableBatteryOptimizations();
|
||||||
|
break;
|
||||||
|
case "webviewoptimizations":
|
||||||
disableWebViewOptimizations();
|
disableWebViewOptimizations();
|
||||||
break;
|
break;
|
||||||
case "background":
|
case "background":
|
||||||
@ -179,6 +186,26 @@ class BackgroundExt {
|
|||||||
thread.start();
|
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.
|
* Excludes the app from the recent tasks list.
|
||||||
*/
|
*/
|
||||||
|
@ -104,7 +104,8 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
case "disable":
|
case "disable":
|
||||||
disableMode();
|
disableMode();
|
||||||
break;
|
break;
|
||||||
case "optimizations":
|
case "webviewoptimizations":
|
||||||
|
case "batteryoptimizations":
|
||||||
case "background":
|
case "background":
|
||||||
case "foreground":
|
case "foreground":
|
||||||
case "tasklist":
|
case "tasklist":
|
||||||
@ -303,5 +304,4 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
|
|
||||||
cordova.getActivity().runOnUiThread(() -> webView.loadUrl("javascript:" + js));
|
cordova.getActivity().runOnUiThread(() -> webView.loadUrl("javascript:" + js));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -320,5 +320,4 @@ public class ForegroundService extends Service {
|
|||||||
{
|
{
|
||||||
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -160,7 +160,20 @@ exports.disableWebViewOptimizations = function()
|
|||||||
{
|
{
|
||||||
if (this._isAndroid)
|
if (this._isAndroid)
|
||||||
{
|
{
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'optimizations', []);
|
cordova.exec(null, null, 'BackgroundMode', 'webviewoptimizations', []);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables battery optimazation mode for the app.
|
||||||
|
*
|
||||||
|
* @return [ Void ]
|
||||||
|
*/
|
||||||
|
exports.disableBatteryOptimizations = function()
|
||||||
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
|
cordova.exec(null, null, 'BackgroundMode', 'batteryoptimizations', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user