Android Question [B4XPages] Starting an APP with Intent in Kiosk Mode causes glitches (not in Normale Mode)

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone, I'm in the finishing phase of my project now, where I'm testing the overall functionalities (including the real scenario for the kiosk mode)... but I just encountered a big big big problem I do not know how to solve.
The project is partially described in this post.

AT THE MOMENT:
There is a Watchdog APP (A) that periodically sends an Intent to starts the Main APP (B) (in this way if it crashes can be resumed automatically) as @Erel suggested in this post, where, it is said that launching an already-visible app will have no effects.

With this snippet (is it correct for the kiosk mode?)
B4X:
Sub Timer_Tick
    Dim In As Intent
    Dim pm As PackageManager
    In = pm.GetApplicationIntent("abc.def")
    If In.IsInitialized Then
        StartActivity(In)
    End If
End Sub
This work correctly ONLY when the device is not in Kiosk Mode.



IN KIOSK MODE:
Every time that A sends an intent to start B, even if it is already in the foreground everything glitches because these events are fired
B4X:
WDT Service //<--- Intent that starts Main App (B) has arrived
** Activity (main) Pause event (activity is not paused). **
*** home: B4XPage_Disappear [home]
*** mainpage: B4XPage_Background [home]
RFID.StopServiceScheduler
** Activity (main) Resume **
*** mainpage: B4XPage_Foreground [home]
*** home: B4XPage_Appear [home]
^^^ This does not happen if Kiosk Mode is disabled ^^^


I hope someone can help me because this was an extremely unexpected behavior.

Thanks in advance
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
A possible workaround is to make main app write the current time to a text file every 10 seconds and watchdog app will read it every 10 seconds. If the time wasn't updated for 30 seconds then send the intent.
Make sure to read and write with Try / Catch as the file might be locked in some edge cases.

You should use this feature to access the secondary storage: https://www.b4x.com/android/forum/t...cess-internal-external-storage-sdk-30.130411/
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
A possible workaround is to make main app write the current time to a text file every 10 seconds and watchdog app will read it every 10 seconds. If the time wasn't updated for 30 seconds then send the intent.
Make sure to read and write with Try / Catch as the file might be locked in some edge cases.

You should use this feature to access the secondary storage: https://www.b4x.com/android/forum/t...cess-internal-external-storage-sdk-30.130411/
Ok, this could be a extremis-solution.

I was looking for a way to get the list of currently running apps from the watchdog and I found this snippet on stackoverflow but I do not know how to translate it into B4A.
Java:
private String getProcessName() {
    String foregroundProcess = "";
    ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
    // Process running
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(USAGE_STATS_SERVICE);
        long time = System.currentTimeMillis();
        // We get usage stats for the last 10 seconds
        List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time);
        // Sort the stats by the last time used
        if(stats != null) {
            SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
            for (UsageStats usageStats : stats) {
                mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
                Log.d("RunningAppProcessInfo","Package name : "+usageStats.getPackageName());
            }

        }
    }
}

and also this other thread is worth to take a look at, contains some other code
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
At the moment I solved my issue by re-imagine the Watchdog functioning

I put an "Heartbeat" signal that is sent by the Main APP every second though an Intent to the Watchdog APP
If the Watchdog APP does not receive the Heartbeat within 3 seconds it re-launches the Main APP.
In these way I avoid continuously launching the Main APP

(being able to retrive the running apps btw could be surely a great plus, I saw that DonManfred wrote the library that is needed I think)
 
Upvote 0
Top