mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-22 07:14:54 +00:00
Merged in Menardi/cordova-plugin-run-in-background/android_optimization (pull request #2)
Add more Android battery optimisation functions - Go to Battery Optimization settings screen - Add function to check if battery optimization is enabled or not
This commit is contained in:
parent
0da2006f7c
commit
53f3278722
25
README.md
25
README.md
@ -122,10 +122,33 @@ cordova.plugins.backgroundMode.unlock();
|
||||
|
||||
### Request to disable battery optimizations
|
||||
Starting in Android 8, apps can be put to sleep to conserve battery. When this happens (usually after 5 minutes or so), the background task is killed. This will cause things like MQTT connections to break.
|
||||
|
||||
This method will show a permission prompt for the user (only if the app hasn't been granted permission) to ignore the optimization.
|
||||
|
||||
```js
|
||||
cordova.plugins.backgroundMode.disableWebViewOptimizations();
|
||||
cordova.plugins.backgroundMode.disableBatteryOptimizations();
|
||||
```
|
||||
|
||||
You can also open the battery optimization settings menu directly, and get the user to set it manually. This may be a better option for devices which may ignore the prompt above.
|
||||
|
||||
```js
|
||||
cordova.plugins.backgroundMode.openBatteryOptimizationsSettings();
|
||||
```
|
||||
|
||||
To check if battery optimizations are disabled for the app:
|
||||
|
||||
```js
|
||||
cordova.plugins.backgroundMode.isIgnoringBatteryOptimizations(function(isIgnoring) {
|
||||
...
|
||||
})
|
||||
```
|
||||
|
||||
Additionally, you may find that your JS code begins to run less frequently, or not at all while in the background. This can be due to the webview slowing down its execution due to being in the background. The `disableWebViewOptimizations` function can prevent that, but it's important that it is run _after_ the app goes to the background.
|
||||
|
||||
```js
|
||||
cordova.plugins.backgroundMode.on('activate', function() {
|
||||
cordova.plugins.backgroundMode.disableWebViewOptimizations();
|
||||
});
|
||||
```
|
||||
|
||||
### Notification
|
||||
|
@ -55,6 +55,7 @@ import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY;
|
||||
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.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS;
|
||||
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;
|
||||
@ -90,6 +91,12 @@ public class BackgroundModeExt extends CordovaPlugin {
|
||||
case "battery":
|
||||
disableBatteryOptimizations();
|
||||
break;
|
||||
case "batterysettings":
|
||||
openBatterySettings();
|
||||
break;
|
||||
case "optimizationstatus":
|
||||
isIgnoringBatteryOptimizations(callback);
|
||||
break;
|
||||
case "webview":
|
||||
disableWebViewOptimizations();
|
||||
break;
|
||||
@ -209,6 +216,39 @@ public class BackgroundModeExt extends CordovaPlugin {
|
||||
cordova.getActivity().startActivity(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the Battery Optimization settings screen
|
||||
*/
|
||||
private void openBatterySettings()
|
||||
{
|
||||
if (SDK_INT < M)
|
||||
return;
|
||||
|
||||
Activity activity = cordova.getActivity();
|
||||
Intent intent = new Intent(ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
|
||||
|
||||
cordova.getActivity().startActivity(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the Battery Optimization settings screen
|
||||
*
|
||||
* @param callback The callback to invoke.
|
||||
*/
|
||||
private void isIgnoringBatteryOptimizations(CallbackContext callback)
|
||||
{
|
||||
if (SDK_INT < M)
|
||||
return;
|
||||
|
||||
Activity activity = cordova.getActivity();
|
||||
String pkgName = activity.getPackageName();
|
||||
PowerManager pm = (PowerManager)getService(POWER_SERVICE);
|
||||
boolean isIgnoring = pm.isIgnoringBatteryOptimizations(pkgName);
|
||||
PluginResult res = new PluginResult(Status.OK, isIgnoring);
|
||||
|
||||
callback.sendPluginResult(res);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the system settings dialog where the user can tweak or turn off any
|
||||
* custom app start settings added by the manufacturer if available.
|
||||
|
@ -177,6 +177,38 @@ exports.disableBatteryOptimizations = function()
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Opens the system settings screen for battery optimization, allowing the user to
|
||||
* manually change the optimization settings.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.openBatteryOptimizationsSettings = function()
|
||||
{
|
||||
if (this._isAndroid)
|
||||
{
|
||||
cordova.exec(null, null, 'BackgroundModeExt', 'batterysettings', []);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Opens the system settings screen for battery optimization, allowing the user to
|
||||
* manually change the optimization settings.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.isIgnoringBatteryOptimizations = function(callback)
|
||||
{
|
||||
if (this._isAndroid)
|
||||
{
|
||||
cordova.exec(callback, null, 'BackgroundModeExt', 'optimizationstatus', []);
|
||||
}
|
||||
else
|
||||
{
|
||||
callback(true);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Opens the system settings dialog where the user can tweak or turn off any
|
||||
* custom app start settings added by the manufacturer if available.
|
||||
|
Loading…
Reference in New Issue
Block a user