Android Question How to check if an application is active.

warayTek

Member
Licensed User
I am testing this app to open a particular package name., and check if it is running. But the results I get is "Not active".
Here is the code I'm using.

B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private lv As ListView
    Private packList As List
    Private MP As MediaPlayer
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    packList.Initialize
    Root = Root1
    Root.LoadLayout("MainPage")
    MP.Initialize2("MP")
    MP.Load(File.DirAssets, "beep-08b.mp3")
    Dim mi As B4AMenuItem
    mi = B4XPages.AddMenuItem(Me, "Erase")
    mi.AddToBar = True
    mi.Bitmap = xui.LoadBitmapResize(File.DirAssets, "eraser.png", 50dip, 50dip, True)
    Sleep(1000)
    lv.Clear
    Dim pm As PackageManager
    Dim packages As List
    packages = pm.GetInstalledPackages
    For i = 0 To packages.Size - 1
        ProgressDialogShow2("Loading...",False)
        Try
        Dim p As String = packages.Get(i)
        Dim bdw As BitmapDrawable = pm.GetApplicationIcon(p)
        lv.AddTwoLinesAndBitmap(pm.GetApplicationLabel(p),packages.Get(i),bdw.Bitmap)
        packList.Add(packages.Get(i))
        Log(packages.Get(i))
        Catch
            Log("error")
        End Try
    Next
    ProgressDialogHide
End Sub

Sub IsAppActive(packageName As String) As Boolean
    Dim jo As JavaObject
    ' Initialize JavaObject to get the current activity manager
    jo.InitializeContext
    Dim activityManager As JavaObject = jo.RunMethod("getSystemService", Array("activity"))
   
    ' Get the list of currently running tasks
    Dim tasks As List = activityManager.RunMethod("getRunningAppProcesses", Null)
   
    For Each task As JavaObject In tasks
        Dim activePackageName As String = task.GetField("processName")
       
        ' Check if the active package name matches the target package (apollo.com)
        If activePackageName = packageName Then
            ' If the app is found, return True
            Return True
        End If
    Next
   
    ' Return False if the app is not found in the running tasks
    Return False
End Sub


Sub lv_ItemLongClick (Position As Int, Value As Object)
    MP.Play
    Dim GetrIndex As Int
    Dim GetrText As String
    GetrIndex = Position
    GetrText = Value
    Log(packList.Get(Position))
    OpenApp(packList.Get(Position))
    Sleep(1200)
    If IsAppActive(packList.Get(Position)) = True Then
        Log("Active")
    Else
        Log("Not active")
    End If
End Sub

Sub OpenApp(packageName As String)
    Dim pm As PackageManager
    Dim intent As Intent
   
    ' Try to create an intent to launch the application
    Try
        intent = pm.GetApplicationIntent(packageName)
        If intent.IsInitialized Then
            StartActivity(intent)
        Else
            Log("Application with package " & packageName & " not found.")
        End If
    Catch
        Log("Error: Unable to open the application.")
    End Try
End Sub
I appreciate any help. Thanks!
 

drgottjr

Expert
Licensed User
Longtime User
mostly a noop these days. the ai will be happy to tell you why.
 
Upvote 0
Top