Closing applications after opening in Kiosk mode

Solved
TW
Tim Wright
tmplant

Hi all, 

Is there a script or anything that makes apps close once the user goes back to there kiosk instead of still being open in the background ?

a year ago
SOTI MobiControl
ANSWERS

Not in general, you could close the app if not in foreground with a scheduled task via javascript (you need to play around with loops in JS so you don't run into issues on server side running the script too frequent).

https://www.soti.net/mc/help/javascriptapi/en/mobicontrol.app.html

But never done this and not sure how much this would affect the server or device performance also depending maybe on how many devices are managed.

Solution
RC
Raymond Chan Diamond Contributor
a year ago

I just noted this discussion thread a couple minutes ago, and was very surprised.   Although the discussion has already been closed with a so-called solution marked,  I believe there is a need to say something here, as other audience in this forum may get misled by both the question and answer, and might have big problems with enterprise program/data in their own implementation if blindly following what has been said.

First of all, the question is not clear in several respects.  When an app is "closed",  the normal meaning is "graceful" termination often initiated by the software end-user via provided UI, by  full completion of programmed task(s), or by scheduled/asynchronous system or external event(s) selected.  For Android program,  There are relevant API's for the program itself  to detect whether iit s still running in the foreground and automatically terminate itself gracefully if it is in the background (e.g. when the user goes back to the kiosk as mentioned in Tim's description).

On the other hand, there is mechanism for MDM or kernel/system to forcefully "stop" (or "kill") a program.  This is not graceful termination and there is a possibility that some data will be lost or incorrect because the program may be interrupted in the middle of what it is working on. Strictly speaking, "Stopping/Killing" an app is very different from "closing" an app.

As Tim has not given any details on his target app or what it is doing, or whether forceful termination of the program is acceptable, it is hard to answer his question without getting any clarification from him.

Unfortunately, Rafael followed his usual style as in many other threads, commenting on lots of things without clarifying the scenario, and often made lots of guesses or assumptions without mentioning them when posting his answers.  I don't believe he will use this somewhat irresponsible style to serve real customers in his own company, risking his job when disaster happen to hundreds of customer devices when his customer follows some misleading/wrong recommendations.  I look forward to seeing improvements in his future posts in other threads.

Now, returning to the answer part, which depends on lots of things.

Firstly, if the app of interest include code to receive Android intent for graceful termination of the app, then a combination of Mobicontrol legacy & javascript together can be used to check whether the program is still in foreground and send the relevant intent if it is not.

Secondly, if forceful stopping/killing of the program is acceptable (i.e. thoroughly tested to confirm no mission critical data will be lost/corrupted, etc.), then the javascript library mentioned by Rafael can be used.

However, using a scheduled task to run either of the above script can drain empty the device battery power more quickly and incur unpredictable or unacceptably long delay/response.  Initiating such script from the server as mentioned by Rafael is likely not sensible because the mechanism will not work when the device is offline, which is not likely acceptable for smooth user interaction involving very frequent kiosk home-screen entry/exit events.

If the app is an in-house developed enterprise app with known details of its operations & features, some change(s) of the code base together with relevant MobiControl script (legacy/javascript) may be used together to solve the above mentioned problem(s) more optimally.

If Tim later provides more details on his apps and use case and limitations, maybe some better solution(s) can be proposed further.

Just to clear out one thing you mentioned "... a combination of legacy and javascript ... send the relevant intent..." is not true.

It can all be done via javascript API from Soti:

To detect which app is in foreground: https://www.soti.net/mc/help/javascriptapi/en/mobicontrol.app.html#.foregroundActivities

To "force" close the app: https://www.soti.net/mc/help/javascriptapi/en/mobicontrol.app.html#.stop

And anything else is JS with a loop and dependencies.
Just a small example to start an app if it's NOT in foreground for ~15 seconds and you are in user mode:

var i = 0;
var AppName = 'your.budle.id';
var Runs = false;
var foregroundActivities = mobicontrol.app.foregroundActivities;
function Checking() {
    setTimeout(function () {
        if (mobicontrol.agent.isUserMode() == true) {
            foregroundActivities = mobicontrol.app.foregroundActivities;
            if (foregroundActivities != null) {
                foregroundActivities.forEach(function (activity) {
                    if (activity.packageName != AppName) {
                        if (!(Runs)) {
                            Runs = true;
                            setTimeout(RunIt, 14900);
                        }
                    }
                });
            }
        }
        i++;
        if (i < 60) {
            Checking();
        }
    }, 950)
}
function RunIt() {
    if (mobicontrol.agent.isUserMode() == true) {
        mobicontrol.app.start(AppName);
    }
    Runs = false;
}

Checking();

You don't need to do this via a scheduled task (but could be but with your mentioned problems).
You could add it as a script to be ran if an app is started (or running in general when the kioskis shown but would not recommend that) included in the template itself.

But to be honest i was also surprised that it got marked as solution so fast.

RC
Raymond Chan Diamond Contributor
a year ago

Rafael,

Tim asked about "closing" an app running in the background when the kiosk home-screen is in the foreground, and your example javascript is doing something totally different and irrelevant here..

When I mentioned about using intent, the primary goal is to ensure GRACEFUL termination by the app code itself, so that running critical section/task will be completed before the program SAFELY terminate itself.  

Audience with some basic programming skills and common sense will understand  what I am talking about in this and my last posts in this thread.

Forced stopping/Killing an app may be OK in Tim's use case specific to the app he was working on, and he thus marked your post as HIS solution, and maybe he overlooked some side-effects and will provide more details for others to proposed better solution in the future.

My last post is just a reminder that 'closing" and "forced-stopping/killing" a running program are not the same, and may need to be considered for different apps of interest by other audience of this forum when handling their own case.

Lots of people posted stupid or unclear questions or gave insufficient background details in many  previous discussion threads in this open forum.    As professional and responsible forum member to contribute and help,  shouldn't clarifications be requested first in such cases before recommending solution?  Otherwise, the proposed solution may be based on incorrect and implicit assumptions different from the real case, and can result in disaster in some extreme cases.  Who is going to be responsible if that happens?