2

In my application if user lock the mobile.I need to navigate to login screen.I have implemented resume event to navigate login screen if user unlock the device.can anybody tell Why Cordova resume event is not firing in power lock mode / Sleep mode in iOS

is there any other event i need to use in lock mode?

P.S It is working in minimizing the app and maximizing the app

user386430
  • 4,837
  • 13
  • 41
  • 45
  • Are you still in the app when pressing the power button? Resume is only fired when a app is retrieved from the background so when you are still in the app resume is never fired despite the fact that you were in lock mode but during this the app has not put into the background. Is a pause event fired after pressing the power button(getting in lock mode)? – Blauharley Dec 24 '15 at 18:15
  • @Blauharley no what event i need to use in power lock mode – user386430 Dec 26 '15 at 05:40
  • Unfortunately there are no more events you can use and pause event is supposed to be fired when an app is put on hold so in your case the app is still active in the foreground because pause is not fired and therefore is still able to do something like active/inactive checks. This IOS behaviour differs from android so you might have to implement a solution for both platforms. – Blauharley Dec 26 '15 at 13:08
  • @Blauharley can you tell how check active or inactive application in java script or sencha – user386430 Dec 27 '15 at 06:28

2 Answers2

0

Although in IOS a resume event is fired when minimizing or maximizing an app by pressing the home button, it appears that a resume event is not fired when "closing" or "staring" the app by pressing the power button at least in IOS.

A possible JS-solution might be to check for inactivity. Let's say when an app has not not received any events triggered by an user for some time(30 seconds and if no real pause-event has been fired since) for instance click/touch-events then it can be assumed that the app can still execute some code(so it's still in foreground) and is "paused":

// threshold for inactivity state
var idleTimeout = 30000;
// variable that holds the time in seconds, which indicates how long the app has not received certain events
var timeInSecondsPassed = 0;
// interval instance
var intervalInstance = null;
// variable to handle the transition from "pause" to "resume" state
var inPauseState = false;

function startPauseListener() {
    timeInSecondsPassed = 0;
    var resetPassedTime = function(){
        timeInSecondsPassed = 0;
        // has the app reached the "pause" state and 
        // currently receiving certain events -> the "resume" state is reached
        if(inPauseState){
           inPauseState = false;
           // the "resume" state is reached here 
           // so the same code might be executed here as it is in the resume-listener
        }
    };
    document.ontouchstart = resetPassedTime;
    document.onclick = resetPassedTime;
    document.onscroll = resetPassedTime;
    document.onkeypress = resetPassedTime;
    intervalInstance = setInterval(checkPauseState,1000);
}

function clearPauseListener() {
    clearInterval(intervalInstance);
    timeInSecondsPassed = 0;
}

function checkPauseState() {
    timeInSecondsPassed += 1000;
    if (timeInSecondsPassed >= idleTimeout) {
       inPauseState = true;
       timeInSecondsPassed = 0;
       // run further code here to handle "pause" state
       // at this point it is assumed as soon as the app receives click/touch-events again a "resume" state is reached.
    }
}

function onDeviceReady() {
    // handle android devices so that the interval is stopped when a real pause event is fired and started when a real resume event is fired
    document.addEventListener("resume", function(){
        startPauseListener();
        // your actual code to handle real resume events
    }, false);

    document.addEventListener("pause", function(){
        clearPauseListener();
    }, false);
}

It has to be noted that when the app is really paused so a pause event is fired the code above is not run in IOS but in android so that's why you might have to handle this szenario in android differently by taking advandage of both resume and pause-Listener for in android when the app is minimized by the home button the interval would still be executed and consumes CPU in the background.

And please also note that it's only a kind of concept-code and not tested in any device!!!

Hope this helps.

Blauharley
  • 4,186
  • 6
  • 28
  • 47
0

There's an iOS-specific event called active that "detects when users disable the Lock button to unlock the device with the app running in the foreground".

Check the documentation at the bottom of the resume doc page:

https://cordova.apache.org/docs/en/5.1.1/cordova/events/events.resume.html

adamvert
  • 615
  • 5
  • 15