mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-15 03:54:54 +00:00
New method isActive
to receive if mode is active
This commit is contained in:
parent
fc6e6cf51d
commit
8f2c75321c
@ -1,7 +1,8 @@
|
|||||||
## ChangeLog
|
## ChangeLog
|
||||||
#### Version 0.6.1 (not yet released)
|
#### Version 0.6.1 (not yet released)
|
||||||
- [enhancement:] Set default settings through `setDefaults`.
|
- [enhancement:] Set default settings through `setDefaults`.
|
||||||
- [enhancement:] New method `isEnabled`
|
- [enhancement:] New method `isEnabled` to receive if mode is enabled.
|
||||||
|
- [enhancement:] New method `isActive` to receive if mode is active.
|
||||||
- [bugfix:] Events caused thread collision.
|
- [bugfix:] Events caused thread collision.
|
||||||
|
|
||||||
|
|
||||||
|
31
README.md
31
README.md
@ -70,7 +70,8 @@ More informations can be found [here][PGB_plugin].
|
|||||||
## ChangeLog
|
## ChangeLog
|
||||||
#### Version 0.6.1 (not yet released)
|
#### Version 0.6.1 (not yet released)
|
||||||
- [enhancement:] Set default settings through `setDefaults`.
|
- [enhancement:] Set default settings through `setDefaults`.
|
||||||
- [enhancement:] New method `isEnabled`
|
- [enhancement:] New method `isEnabled` to receive if mode is enabled.
|
||||||
|
- [enhancement:] New method `isActive` to receive if mode is active.
|
||||||
- [bugfix:] Events caused thread collision.
|
- [bugfix:] Events caused thread collision.
|
||||||
|
|
||||||
#### Further informations
|
#### Further informations
|
||||||
@ -82,17 +83,18 @@ More informations can be found [here][PGB_plugin].
|
|||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
The plugin creates the object ```cordova.plugins.backgroundMode``` with the following methods:
|
The plugin creates the object `cordova.plugins.backgroundMode` with the following methods:
|
||||||
|
|
||||||
1. [backgroundMode.enable][enable]
|
1. [backgroundMode.enable][enable]
|
||||||
2. [backgroundMode.disable][disable]
|
2. [backgroundMode.disable][disable]
|
||||||
3. [backgroundMode.isEnabled][is_enabled]
|
3. [backgroundMode.isEnabled][is_enabled]
|
||||||
3. [backgroundMode.getDefaults][android_specifics]
|
4. [backgroundMode.isActive][is_active]
|
||||||
4. [backgroundMode.setDefaults][android_specifics]
|
5. [backgroundMode.getDefaults][android_specifics]
|
||||||
2. [backgroundMode.configure][android_specifics]
|
6. [backgroundMode.setDefaults][android_specifics]
|
||||||
3. [backgroundMode.onactivate][onactivate]
|
7. [backgroundMode.configure][android_specifics]
|
||||||
4. [backgroundMode.ondeactivate][ondeactivate]
|
8. [backgroundMode.onactivate][onactivate]
|
||||||
5. [backgroundMode.onfailure][onfailure]
|
9. [backgroundMode.ondeactivate][ondeactivate]
|
||||||
|
10. [backgroundMode.onfailure][onfailure]
|
||||||
|
|
||||||
### Plugin initialization
|
### Plugin initialization
|
||||||
The plugin and its methods are not available before the *deviceready* event has been fired.
|
The plugin and its methods are not available before the *deviceready* event has been fired.
|
||||||
@ -125,10 +127,17 @@ cordova.plugins.backgroundMode.disable();
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Receive if the background mode is enabled
|
### Receive if the background mode is enabled
|
||||||
The `backgroundMode.isActivated` interface can be used to get the information if the background mode is enabled or disabled.
|
The `backgroundMode.isEnabled` interface can be used to get the information if the background mode is enabled or disabled.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var enabled = cordova.plugins.backgroundMode.isEnabled();
|
cordova.plugins.backgroundMode.isEnabled(); // => boolean
|
||||||
|
```
|
||||||
|
|
||||||
|
### Receive if the background mode is active
|
||||||
|
The `backgroundMode.isActive` interface can be used to get the information if the background mode is active.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
cordova.plugins.backgroundMode.isActive(); // => boolean
|
||||||
```
|
```
|
||||||
|
|
||||||
### Get informed when the background mode has been activated
|
### Get informed when the background mode has been activated
|
||||||
@ -240,6 +249,8 @@ This software is released under the [Apache 2.0 License][apache2_license].
|
|||||||
[changelog]: CHANGELOG.md
|
[changelog]: CHANGELOG.md
|
||||||
[enable]: #prevent_the_app_from_going_to_sleep_in_background
|
[enable]: #prevent_the_app_from_going_to_sleep_in_background
|
||||||
[disable]: #pause_the_app_while_in_background
|
[disable]: #pause_the_app_while_in_background
|
||||||
|
[is_enabled]: #receive_if_the_background_mode_is_enabled
|
||||||
|
[is_active]: #receive_if_the_background_mode_is_active
|
||||||
[android_specifics]: #android_customization
|
[android_specifics]: #android_customization
|
||||||
[onactivate]: #get_informed_when_the_background_mode_has_been_activated
|
[onactivate]: #get_informed_when_the_background_mode_has_been_activated
|
||||||
[ondeactivate]: #get_informed_when_the_background_mode_has_been_deactivated
|
[ondeactivate]: #get_informed_when_the_background_mode_has_been_deactivated
|
||||||
|
@ -290,12 +290,18 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
eventName = "deactivate"; break;
|
eventName = "deactivate"; break;
|
||||||
default:
|
default:
|
||||||
eventName = "failure";
|
eventName = "failure";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final String js = String.format("setTimeout('%s.on%s(%s)',0)",
|
String active = event == Event.ACTIVATE ? "true" : false;
|
||||||
|
|
||||||
|
String flag = String.format("%s._isActive=%s;",
|
||||||
|
JS_NAMESPACE, active);
|
||||||
|
|
||||||
|
String fn = String.format("setTimeout('%s.on%s(%s)',0);",
|
||||||
JS_NAMESPACE, eventName, params);
|
JS_NAMESPACE, eventName, params);
|
||||||
|
|
||||||
|
final String js = flag + fn;
|
||||||
|
|
||||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -23,10 +23,10 @@
|
|||||||
|
|
||||||
@implementation APPBackgroundMode
|
@implementation APPBackgroundMode
|
||||||
|
|
||||||
NSString *const kAPPBackgroundModeNamespace = @"cordova.plugins.backgroundMode";
|
NSString *const kAPPBackgroundJsNamespace = @"cordova.plugins.backgroundMode";
|
||||||
NSString *const kAPPBackgroundModeActivateEvent = @"activate";
|
NSString *const kAPPBackgroundEventActivate = @"activate";
|
||||||
NSString *const kAPPBackgroundModeDeactivateEvent = @"deactivate";
|
NSString *const kAPPBackgroundEventDeactivate = @"deactivate";
|
||||||
NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
NSString *const kAPPBackgroundEventFailure = @"failure";
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
#pragma mark Initialization methods
|
#pragma mark Initialization methods
|
||||||
@ -104,7 +104,7 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
|||||||
- (void) keepAwake {
|
- (void) keepAwake {
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
[audioPlayer play];
|
[audioPlayer play];
|
||||||
[self fireEvent:kAPPBackgroundModeActivateEvent withParams:NULL];
|
[self fireEvent:kAPPBackgroundEventActivate withParams:NULL];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +118,7 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (audioPlayer.isPlaying) {
|
if (audioPlayer.isPlaying) {
|
||||||
[self fireEvent:kAPPBackgroundModeDeactivateEvent withParams:NULL];
|
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL];
|
||||||
}
|
}
|
||||||
|
|
||||||
[audioPlayer pause];
|
[audioPlayer pause];
|
||||||
@ -166,7 +166,7 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
|||||||
* Restart playing sound when interrupted by phone calls.
|
* Restart playing sound when interrupted by phone calls.
|
||||||
*/
|
*/
|
||||||
- (void) handleAudioSessionInterruption:(NSNotification*)notification {
|
- (void) handleAudioSessionInterruption:(NSNotification*)notification {
|
||||||
[self fireEvent:kAPPBackgroundModeDeactivateEvent withParams:NULL];
|
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL];
|
||||||
[self keepAwake];
|
[self keepAwake];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,8 +175,15 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
|||||||
*/
|
*/
|
||||||
- (void) fireEvent:(NSString*)event withParams:(NSString*)params
|
- (void) fireEvent:(NSString*)event withParams:(NSString*)params
|
||||||
{
|
{
|
||||||
NSString* js = [NSString stringWithFormat:@"setTimeout('%@.on%@(%@)',0)",
|
NSString* active = [event isEqualToString:kAPPBackgroundEventActivate] ? @"true" : @"false";
|
||||||
kAPPBackgroundModeNamespace, event, params];
|
|
||||||
|
NSString* flag = [NSString stringWithFormat:@"%@._isActive=%@;",
|
||||||
|
kAPPBackgroundJsNamespace, active];
|
||||||
|
|
||||||
|
NSString* fn = [NSString stringWithFormat:@"setTimeout('%@.on%@(%@)',0);",
|
||||||
|
kAPPBackgroundJsNamespace, event, params];
|
||||||
|
|
||||||
|
NSString* js = [flag stringByAppendingString:fn];
|
||||||
|
|
||||||
[self.commandDelegate evalJs:js];
|
[self.commandDelegate evalJs:js];
|
||||||
}
|
}
|
||||||
|
@ -44,10 +44,17 @@ channel.onCordovaReady.subscribe(function () {
|
|||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*
|
*
|
||||||
* Flag indicated if the mod is enabled.
|
* Flag indicated if the mode is enabled.
|
||||||
*/
|
*/
|
||||||
exports._isEnabled = true;
|
exports._isEnabled = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*
|
||||||
|
* Flag indicated if the mode is active.
|
||||||
|
*/
|
||||||
|
exports._isActive = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*
|
*
|
||||||
@ -125,7 +132,7 @@ exports.configure = function (options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the mode is enabled or not.
|
* If the mode is enabled or disabled.
|
||||||
*
|
*
|
||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
@ -133,6 +140,15 @@ exports.isEnabled = function () {
|
|||||||
return this._isEnabled;
|
return this._isEnabled;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the mode is active.
|
||||||
|
*
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
exports.isActive = function () {
|
||||||
|
return this._isActive;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the background mode has been activated.
|
* Called when the background mode has been activated.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user