Android Question How to know if a service is running using OS Library?

Sabotto

Well-Known Member
Licensed User
If I want to know if a service (OpenVPN) is running on the smartphone, which instruction should I use? And how? I tried with
B4X:
Dim l1,l2,l3 As List
l1.Initialize
l2.Initialize
l3.Initialize
   
OS.RunningServiceInfo(10,l1,l2,l3)  
'and
OS.RunningAppProcessInfo(l1,l2,l3)
'and
l1=OS.getRunningTasks(10)
'with 
Log(L1)
Log(L2)
Log(L3)
but it doesn't return anything to let me know that service is running

i get
b4a.example : 25328
(ArrayList) [b4a.example]
(ArrayList) [25328]
(ArrayList) [b4a.example: 25328]
b4a.example : 25328
(ArrayList) [b4a.example]
(ArrayList) [25328]
(ArrayList) [b4a.example: 25328]

with
B4X:
l1=OS.getRunningTasks(10)
    For Each s As String In l1
        Log(s)
    Next
i get
TaskInfo{userId=0 stackId=427 taskId=15749 displayId=0 isRunning=true baseIntent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=b4a.example/.main } baseActivity=ComponentInfo{b4a.example/b4a.example.main} topActivity=ComponentInfo{b4a.example/b4a.example.main} origActivity=null realActivity=ComponentInfo{b4a.example/b4a.example.main} numActivities=1 lastActiveTime=837425480 supportsSplitScreenMultiWindow=true resizeMode=1
TaskInfo{userId=0 stackId=0 taskId=1 displayId=0 isRunning=true baseIntent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000300 cmp=com.huawei.android.launcher/.unihome.UniHomeLauncher } baseActivity=ComponentInfo{com.huawei.android.launcher/com.huawei.android.launcher.unihome.UniHomeLauncher} topActivity=ComponentInfo{com.huawei.android.launcher/com.huawei.android.launcher.unihome.UniHomeLauncher} origActivity=null realActivity=ComponentInfo{com.huawei.android.launcher/com.huawei.android.launcher.unihome.UniHomeLauncher} numActivities=1 lastActiveTime=837417131 supportsSplitScreenMultiWindow=true resizeMode=2
 

DonManfred

Expert
Licensed User
Longtime User
I guess you are not using targetsdk 30 or similar...
If you would you would get a no permission error i guess.

Which library EXACTLY are you using?
 
Upvote 0

Sabotto

Well-Known Member
Licensed User
I have found the library OSLibrary 1.4 here
In my manifest
B4X:
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="29"/>
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
This is a very old library that uses non-recommended API calls. It was a bit dodgy in its day and is now well out of date.

getRunningServices and RunningServiceInfo were deprecated in API level 26 and are no longer available

getRunningTasks and RunningTaskInfo were deprecated in API level 21 and are no longer available

RunningAppProcessInfo will not list services.
 
Last edited:
Upvote 0

Sabotto

Well-Known Member
Licensed User
If it is your app you can send an intent to the app and the app reppond with another intent.
If i remember correctly you already got this suggestion.
No, it's not my app.
From my app, I want to know if a service (OpenVPN) is already started, because my app cannot work if that service is not started.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
There is apparently an AIDL API you could use
GitHub - schwabe/ics-openvpn: OpenVPN for Android
Controlling from external apps
There is the AIDL API for real controlling (see developing section). Due to high demand also the Activities de.blinkt.openvpn.api.DisconnectVPN and de.blinkt.openvpn.api.ConnectVPN exist. It uses de.blinkt.openvpn.api.profileName as extra for the name of the VPN profile.
You're on your own from here ;)
 
Upvote 0

Sabotto

Well-Known Member
Licensed User
Unfortunately , I did not understand how to use it. I'm a beginner
I can launch the service with these lines,
B4X:
Dim i As Intent
i.Initialize(i.ACTION_MAIN, "")
i.SetComponent("de.blinkt.openvpn/.LaunchVPN")
i.PutExtra("de.blinkt.openvpn.shortcutProfileName", "VPN_Telecontrollo")
StartActivity(i)
but before doing that I would like to make sure it is not already running
 
Upvote 0

Sabotto

Well-Known Member
Licensed User
For a matter of elegance. If it is not already running, I inform the user that I will start the service

You gave me an idea. However, I activate the service by informing the user that if it is not already active it will be activated. And when I exit the app I deactivate it. At this point I no longer need to know if it is not already active (except for my knowledge, i.e. how to know if a certain service is already running)
 
Last edited:
Upvote 0

Sabotto

Well-Known Member
Licensed User
No. My idea isn't good. Because after I launch the service, I have to wait for the VPN to be active (it takes a few seconds), before testing my IPs. To find out if it is active, I must therefore be sure that it is running
 
Upvote 0
Top