How to detect if another app is running in the background

648 views
Skip to first unread message

Alan Platt

unread,
Feb 28, 2024, 9:46:08 AM2/28/24
to DroidScript
Hi all, I'm trying to use Droidscript to check whether an app is running and if it isn't start it. I'm sure in the past I used app.GetRunningApps() to return a list of apps currently open on the tablet and parse it to find the one I'm looking for. This now only seems to return a single app (the one I've run this command from). Am I doing something stupid, or has this function been rendered useless by recent Android security sandboxing?
Is there another way of doing this?  I just want to check if com.ternux is open and running!

Dave

unread,
Feb 28, 2024, 11:15:12 AM2/28/24
to DroidScript
Hi Alan,

Unfortunately this feature has now been blocked in Android and only returns your own app :(

I can think of two possible ways around this - 

1. In Termux, start a little script that open up a socket/websocket/httpsocket and then check on 127.0.0.1 from the Android app to see if it responds.

2. Put your tablet in full Kiosk (COSU) mode, then it will probably have the rights to view all processes.  Info about Kiosk mode here - 
I hope you like the new Python features btw :)

Alan Platt

unread,
Feb 29, 2024, 5:51:39 AM2/29/24
to DroidScript
Thanks Dave. I'm in the process of implementing the http check suggested using netcat in Termux. One remaining question though, is it possible to start an application in the background? I'm using app.SendIntent to open Termux, but I'd prefer it if my DroidScript app stayed in the foreground when Termux is opened. Is this possible?

Dave

unread,
Feb 29, 2024, 2:48:28 PM2/29/24
to DroidScript
Hmmmm, now we are starting to fight all the recent Android security features...  I think you could use this trick to do what you want.

  glob.overlay = app.CreateOverlay();
    layHead = app.CreateLayout("Linear");
    glob.overlay.AddLayout( layHead, 0.5, 0 );

This is similar to the "Chat Head" sample, it will create an overlay above all apps that is basically invisible because it's less that one pixel in size.  Your app will automatically request the users's permission to allow overlays.  The beneficial side effect of this tiny overlay is that your app will be allowed to launch itself from the background on Android 10+.  So you can set a short timer to bring your app back to the front after starting Termux (in theory), using the app.ToFront() or app.LaunchApp( "com.smartphoneremote.androidscriptfree" ) method.

However I can't guarantee that Termux will keep running in the background as Android is much more aggressive these days about shutting down background processes.

Maybe you don't need Termux and you can achieve what you want using NodeJS inside DS?

Of course if you take the plunge and go with full Kiosk mode, these problems will probably go away and you could add Termux as an 'allowed' kiosk app. Likewise if you root your device.

Regards
David
Reply all
Reply to author
Forward
0 new messages