mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-12-23 13:57:35 +00:00
Detect screen status
This commit is contained in:
parent
8064b8853f
commit
131df973fb
@ -1,4 +1,7 @@
|
||||
## ChangeLog
|
||||
#### Version 0.7.3 (not yet released)
|
||||
- Check if screen is off on Android
|
||||
|
||||
#### Version 0.7.2 (02.02.2017)
|
||||
- Fixed app freeze on iOS using wkwebview-engine
|
||||
- Websocket sample in SampleApp
|
||||
|
35
README.md
35
README.md
@ -47,7 +47,7 @@ Or install from local source:
|
||||
## Usage
|
||||
The plugin creates the object `cordova.plugins.backgroundMode` and is accessible after the *deviceready* event has been fired.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
document.addEventListener('deviceready', function () {
|
||||
// cordova.plugins.backgroundMode is now available
|
||||
}, false);
|
||||
@ -56,14 +56,14 @@ document.addEventListener('deviceready', function () {
|
||||
### Enable the background mode
|
||||
The plugin is not enabled by default. Once it has been enabled the mode becomes active if the app moves to background.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.enable();
|
||||
// or
|
||||
cordova.plugins.backgroundMode.setEnabled(true);
|
||||
```
|
||||
|
||||
To disable the background mode:
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.disable();
|
||||
// or
|
||||
cordova.plugins.backgroundMode.setEnabled(false);
|
||||
@ -72,7 +72,7 @@ cordova.plugins.backgroundMode.setEnabled(false);
|
||||
### Check if running in background
|
||||
Once the plugin has been enabled and the app has entered the background, the background mode becomes active.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.isActive(); // => boolean
|
||||
```
|
||||
|
||||
@ -81,12 +81,12 @@ A non-active mode means that the app is in foreground.
|
||||
### Listen for events
|
||||
The plugin fires an event each time its status has been changed. These events are `enable`, `disable`, `activate`, `deactivate` and `failure`.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.on('EVENT', function);
|
||||
```
|
||||
|
||||
To remove an event listeners:
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.un('EVENT', function);
|
||||
```
|
||||
|
||||
@ -96,7 +96,7 @@ cordova.plugins.backgroundMode.un('EVENT', function);
|
||||
### Transit between application states
|
||||
Android allows to programmatically move from foreground to background or vice versa.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.moveToBackground();
|
||||
// or
|
||||
cordova.plugins.backgroundMode.moveToForeground();
|
||||
@ -105,24 +105,33 @@ cordova.plugins.backgroundMode.moveToForeground();
|
||||
### Back button
|
||||
Override the back button on Android to go to background instead of closing the app.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.overrideBackButton();
|
||||
```
|
||||
|
||||
### Recent task list
|
||||
Exclude the app from the recent task list works on Android 5.0+.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.excludeFromTaskList();
|
||||
```
|
||||
|
||||
### Detect screen status
|
||||
The method works async instead of _isAcive()_ or _isEnabled()_.
|
||||
|
||||
```js
|
||||
cordova.plugins.backgroundMode.isScreenOff(function(bool) {
|
||||
...
|
||||
});
|
||||
```
|
||||
|
||||
### Notification
|
||||
To indicate that the app is executing tasks in background and being paused would disrupt the user, the plug-in has to create a notification while in background - like a download progress bar.
|
||||
|
||||
#### Override defaults
|
||||
The title, text and icon for that notification can be customized as below. Also, by default the app will come to foreground when tapping on the notification. That can be changed by setting resume to false. On Android 5.0+, the color option will set the background color of the notification circle. Also on Android 5.0+, setting hidden to false will make the notification visible on lockscreen.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.setDefaults({
|
||||
title: String,
|
||||
text: String,
|
||||
@ -135,7 +144,7 @@ cordova.plugins.backgroundMode.setDefaults({
|
||||
```
|
||||
|
||||
To modify the currently displayed notification
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.configure({ ... });
|
||||
```
|
||||
|
||||
@ -144,7 +153,7 @@ __Note:__ All properties are optional - only override the things you need to.
|
||||
#### Run in background without notification
|
||||
In silent mode the plugin will not display a notification - which is not the default. Be aware that Android recommends adding a notification otherwise the OS may pause the app.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.configure({ silent: true });
|
||||
```
|
||||
|
||||
@ -153,7 +162,7 @@ cordova.plugins.backgroundMode.configure({ silent: true });
|
||||
|
||||
Various APIs like playing media or tracking GPS position in background might not work while in background even the background mode is active. To fix such issues the plugin provides a method to disable most optimizations done by Android/CrossWalk.
|
||||
|
||||
```javascript
|
||||
```js
|
||||
cordova.plugins.backgroundMode.on('activate', function() {
|
||||
cordova.plugins.backgroundMode.disableWebViewOptimizations();
|
||||
});
|
||||
|
@ -25,18 +25,24 @@ 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.os.Build;
|
||||
import android.os.PowerManager;
|
||||
import android.view.View;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaInterface;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.apache.cordova.PluginResult.Status;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import static android.content.Context.ACTIVITY_SERVICE;
|
||||
import static android.content.Context.POWER_SERVICE;
|
||||
|
||||
class BackgroundExt {
|
||||
|
||||
// Weak reference to the cordova interface passed by the plugin
|
||||
@ -48,40 +54,62 @@ class BackgroundExt {
|
||||
/**
|
||||
* Initialize the extension to perform non-background related tasks.
|
||||
*
|
||||
* @param cordova The cordova interface.
|
||||
* @param webView The cordova web view.
|
||||
* @param plugin The cordova plugin.
|
||||
*/
|
||||
private BackgroundExt(CordovaInterface cordova, CordovaWebView webView) {
|
||||
this.cordova = new WeakReference<CordovaInterface>(cordova);
|
||||
this.webView = new WeakReference<CordovaWebView>(webView);
|
||||
private BackgroundExt(CordovaPlugin plugin) {
|
||||
this.cordova = new WeakReference<CordovaInterface>(plugin.cordova);
|
||||
this.webView = new WeakReference<CordovaWebView>(plugin.webView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the request asynchronous.
|
||||
*
|
||||
* @param plugin The cordova plugin.
|
||||
* @param action The action to execute.
|
||||
* @param callback The callback context used when
|
||||
* calling back into JavaScript.
|
||||
*/
|
||||
@SuppressWarnings("UnusedParameters")
|
||||
static void execute (CordovaPlugin plugin, final String action,
|
||||
final CallbackContext callback) {
|
||||
|
||||
final BackgroundExt ext = new BackgroundExt(plugin);
|
||||
|
||||
plugin.cordova.getThreadPool().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ext.execute(action, callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the request.
|
||||
*
|
||||
* @param action The action to execute.
|
||||
* @param cordova The cordova interface.
|
||||
* @param webView The cordova web view.
|
||||
* @param action The action to execute.
|
||||
* @param callback The callback context used when
|
||||
* calling back into JavaScript.
|
||||
*/
|
||||
static void execute(String action, CordovaInterface cordova,
|
||||
CordovaWebView webView) {
|
||||
|
||||
BackgroundExt ext = new BackgroundExt(cordova, webView);
|
||||
private void execute (String action, CallbackContext callback) {
|
||||
|
||||
if (action.equalsIgnoreCase("optimizations")) {
|
||||
ext.disableWebViewOptimizations();
|
||||
disableWebViewOptimizations();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("background")) {
|
||||
ext.moveToBackground();
|
||||
moveToBackground();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("foreground")) {
|
||||
ext.moveToForeground();
|
||||
moveToForeground();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("tasklist")) {
|
||||
ext.excludeFromTaskList();
|
||||
excludeFromTaskList();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("dimmed")) {
|
||||
isDimmed(callback);
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,8 +176,7 @@ class BackgroundExt {
|
||||
*/
|
||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||
private void excludeFromTaskList() {
|
||||
ActivityManager am = (ActivityManager) getActivity()
|
||||
.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE);
|
||||
|
||||
if (am == null || Build.VERSION.SDK_INT < 21)
|
||||
return;
|
||||
@ -162,6 +189,26 @@ class BackgroundExt {
|
||||
tasks.get(0).setExcludeFromRecents(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke the callback with information if the screen is on.
|
||||
*
|
||||
* @param callback The callback to invoke.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
private void isDimmed (CallbackContext callback) {
|
||||
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
||||
boolean isDimmed;
|
||||
|
||||
if (Build.VERSION.SDK_INT < 20) {
|
||||
isDimmed = !pm.isScreenOn();
|
||||
} else {
|
||||
isDimmed = !pm.isInteractive();
|
||||
}
|
||||
|
||||
PluginResult result = new PluginResult(Status.OK, isDimmed);
|
||||
callback.sendPluginResult(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* The activity referenced by cordova.
|
||||
*
|
||||
@ -171,4 +218,15 @@ class BackgroundExt {
|
||||
return cordova.get().getActivity();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the requested system service by name.
|
||||
*
|
||||
* @param name The name of the service.
|
||||
*
|
||||
* @return The service instance.
|
||||
*/
|
||||
private Object getService (String name) {
|
||||
return getActivity().getSystemService(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ package de.appplant.cordova.plugin.background;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.IBinder;
|
||||
@ -34,6 +33,8 @@ import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import static android.content.Context.BIND_AUTO_CREATE;
|
||||
|
||||
public class BackgroundMode extends CordovaPlugin {
|
||||
|
||||
// Event types for callbacks
|
||||
@ -93,25 +94,24 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
CallbackContext callback) throws JSONException {
|
||||
|
||||
if (action.equalsIgnoreCase("configure")) {
|
||||
JSONObject settings = args.getJSONObject(0);
|
||||
boolean update = args.getBoolean(1);
|
||||
|
||||
configure(settings, update);
|
||||
configure(args.getJSONObject(0), args.getBoolean(1));
|
||||
callback.success();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
|
||||
if (action.equalsIgnoreCase("enable")) {
|
||||
enableMode();
|
||||
callback.success();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
|
||||
if (action.equalsIgnoreCase("disable")) {
|
||||
disableMode();
|
||||
}
|
||||
else {
|
||||
BackgroundExt.execute(action, cordova, webView);
|
||||
callback.success();
|
||||
return true;
|
||||
}
|
||||
|
||||
callback.success();
|
||||
|
||||
BackgroundExt.execute(this, action, callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -221,15 +221,11 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
if (isDisabled || isBind)
|
||||
return;
|
||||
|
||||
Intent intent = new Intent(
|
||||
context, ForegroundService.class);
|
||||
Intent intent = new Intent(context, ForegroundService.class);
|
||||
|
||||
try {
|
||||
context.bindService(intent,
|
||||
connection, Context.BIND_AUTO_CREATE);
|
||||
|
||||
context.bindService(intent, connection, BIND_AUTO_CREATE);
|
||||
fireEvent(Event.ACTIVATE, null);
|
||||
|
||||
context.startService(intent);
|
||||
} catch (Exception e) {
|
||||
fireEvent(Event.FAILURE, String.format("'%s'", e.getMessage()));
|
||||
@ -244,15 +240,12 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
*/
|
||||
private void stopService() {
|
||||
Activity context = cordova.getActivity();
|
||||
|
||||
Intent intent = new Intent(
|
||||
context, ForegroundService.class);
|
||||
Intent intent = new Intent(context, ForegroundService.class);
|
||||
|
||||
if (!isBind)
|
||||
return;
|
||||
|
||||
fireEvent(Event.DEACTIVATE, null);
|
||||
|
||||
context.unbindService(connection);
|
||||
context.stopService(intent);
|
||||
|
||||
|
@ -181,6 +181,21 @@ exports.overrideBackButton = function () {
|
||||
}, false);
|
||||
};
|
||||
|
||||
/**
|
||||
* If the screen is off.
|
||||
*
|
||||
* @param [ Function ] fn Callback function to invoke with boolean arg.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.isScreenOff = function (fn) {
|
||||
if (this._isAndroid) {
|
||||
cordova.exec(fn, null, 'BackgroundMode', 'dimmed', []);
|
||||
} else {
|
||||
fn(undefined);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* If the mode is enabled or disabled.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user