mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2025-12-13 09:53:22 +00:00
New method isActive to receive if mode is active
This commit is contained in:
@@ -290,12 +290,18 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
eventName = "deactivate"; break;
|
||||
default:
|
||||
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);
|
||||
|
||||
final String js = flag + fn;
|
||||
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
@@ -23,10 +23,10 @@
|
||||
|
||||
@implementation APPBackgroundMode
|
||||
|
||||
NSString *const kAPPBackgroundModeNamespace = @"cordova.plugins.backgroundMode";
|
||||
NSString *const kAPPBackgroundModeActivateEvent = @"activate";
|
||||
NSString *const kAPPBackgroundModeDeactivateEvent = @"deactivate";
|
||||
NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
||||
NSString *const kAPPBackgroundJsNamespace = @"cordova.plugins.backgroundMode";
|
||||
NSString *const kAPPBackgroundEventActivate = @"activate";
|
||||
NSString *const kAPPBackgroundEventDeactivate = @"deactivate";
|
||||
NSString *const kAPPBackgroundEventFailure = @"failure";
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Initialization methods
|
||||
@@ -104,7 +104,7 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
||||
- (void) keepAwake {
|
||||
if (enabled) {
|
||||
[audioPlayer play];
|
||||
[self fireEvent:kAPPBackgroundModeActivateEvent withParams:NULL];
|
||||
[self fireEvent:kAPPBackgroundEventActivate withParams:NULL];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
||||
}
|
||||
|
||||
if (audioPlayer.isPlaying) {
|
||||
[self fireEvent:kAPPBackgroundModeDeactivateEvent withParams:NULL];
|
||||
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL];
|
||||
}
|
||||
|
||||
[audioPlayer pause];
|
||||
@@ -166,7 +166,7 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
||||
* Restart playing sound when interrupted by phone calls.
|
||||
*/
|
||||
- (void) handleAudioSessionInterruption:(NSNotification*)notification {
|
||||
[self fireEvent:kAPPBackgroundModeDeactivateEvent withParams:NULL];
|
||||
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL];
|
||||
[self keepAwake];
|
||||
}
|
||||
|
||||
@@ -175,8 +175,15 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
|
||||
*/
|
||||
- (void) fireEvent:(NSString*)event withParams:(NSString*)params
|
||||
{
|
||||
NSString* js = [NSString stringWithFormat:@"setTimeout('%@.on%@(%@)',0)",
|
||||
kAPPBackgroundModeNamespace, event, params];
|
||||
NSString* active = [event isEqualToString:kAPPBackgroundEventActivate] ? @"true" : @"false";
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user