mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-14 11:34:54 +00:00
Add fix for moveToForeground. Requires new permission for Android 10
This commit is contained in:
parent
5f50704564
commit
4d98947310
@ -81,6 +81,8 @@ cordova.plugins.backgroundMode.un('EVENT', function);
|
||||
### Transit between application states
|
||||
Android allows to programmatically move from foreground to background or vice versa.
|
||||
|
||||
Note: starting with Android 10, you must request the "Draw on Top" permission from the user or the call to `moveToForeground` will silently fail. You can request it with `cordova.plugins.backgroundMode.requestForegroundPermission();`. This permission isn't necessary for `moveToBackground`
|
||||
|
||||
```js
|
||||
cordova.plugins.backgroundMode.moveToBackground();
|
||||
// or
|
||||
|
@ -78,6 +78,7 @@
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
||||
</config-file>
|
||||
|
||||
<source-file
|
||||
|
@ -34,6 +34,7 @@ import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
import android.view.View;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
@ -109,6 +110,9 @@ public class BackgroundModeExt extends CordovaPlugin {
|
||||
case "foreground":
|
||||
moveToForeground();
|
||||
break;
|
||||
case "requestTopPermissions":
|
||||
requestTopPermissions();
|
||||
break;
|
||||
case "tasklist":
|
||||
excludeFromTaskList();
|
||||
break;
|
||||
@ -249,6 +253,20 @@ public class BackgroundModeExt extends CordovaPlugin {
|
||||
callback.sendPluginResult(res);
|
||||
}
|
||||
|
||||
private void requestTopPermissions() {
|
||||
if (SDK_INT >= M) {
|
||||
|
||||
Activity activity = cordova.getActivity();
|
||||
if (Settings.canDrawOverlays(activity.getApplicationContext())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String pkgName = activity.getPackageName();
|
||||
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + pkgName));
|
||||
activity.startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the system settings dialog where the user can tweak or turn off any
|
||||
* custom app start settings added by the manufacturer if available.
|
||||
|
@ -252,6 +252,17 @@ exports.moveToForeground = function()
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Requests permission to "draw on top" which is necessary for the "moveToForeground" method in Android 10+
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.requestForegroundPermission = function() {
|
||||
if (this._isAndroid) {
|
||||
cordova.exec(null, null, 'BackgroundModeExt', 'requestTopPermissions', []);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Exclude the app from the recent tasks list (Android only).
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user