Android Code Snippet Get the currently running activity

This is a small Service Module:

Service Name: Mysvc

Description: Interrogates the activity manager to find out the currently running foreground application.

B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Timer1 As Timer
    Dim ActMan As JavaObject

End Sub
Sub Service_Create
    Timer1.Initialize("Timer1",1000)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Timer1.Enabled = True
    Dim R As Reflector
    R.Target=R.GetContext
    'Get Activity Manager Object
    ActMan = R.RunMethod2("getSystemService","activity","java.lang.String")
End Sub

Sub Service_Destroy
    Timer1.Enabled = False
End Sub
Sub Timer1_Tick
    'From http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service
    Dim TaskInfo As JavaObject = ActMan.RunMethod("getRunningTasks",Array As Object(1))
    Dim CompInfo As JavaObject = TaskInfo.RunMethodJO("get",Array As Object(0)).GetField("topActivity")
    Dim PackageName As String = CompInfo.RunMethod("getPackageName",Null)
    Log(PackageName)
End Sub

Depends on: Reflection and JavaObject

Add this to your manifest:

B4X:
AddPermission(android.permission.GET_TASKS)

Usage in Main:

B4X:
StartService(mySvc)

Tags: Activity Manager Current application
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Yours is a complete Service example though.

Sometimes a one shot solution is simpler. It's a good link and worth mentioning if more functionality is required. Thanks
 

DonManfred

Expert
Licensed User
Longtime User
Description: Interrogates the activity manager to find out the currently running foreground application.

Thank you for this little helper... I copied the heart of this code and tried it with my own app (and to help someone in forum). To test whether i can get my own packagename.
It works. REALLY NICE! Thank you for sharing! :)
 

DonManfred

Expert
Licensed User
Longtime User
Note: Starting from B4A 4+ (i think) this is much easier with the new Application object
B4X:
log(application.PackageName)


Subname: GetPackagename
Author: @stevel05

Description: Gets the packagename of the app running this code (the own app)
Please note that you need the permission GET_TASKS! Add this in the manifesteditor
AddPermission(android.permission.GET_TASKS)
B4X:
Sub GetPackagename
  Dim ActMan As JavaObject
  Dim R As Reflector
  R.Target=R.GetContext
  'Get Activity Manager Object
    ActMan = R.RunMethod2("getSystemService","activity","java.lang.String")
    Dim TaskInfo As JavaObject = ActMan.RunMethod("getRunningTasks",Array As Object(1))
    Dim CompInfo As JavaObject = TaskInfo.RunMethodJO("get",Array As Object(0)).GetField("topActivity")
    Dim PackageName As String = CompInfo.RunMethod("getPackageName",Null)
    Log(PackageName)
    Return PackageName
End Sub

Example
B4X:
dim p as string
p = GetPackagename
log(p)

Depends on: reflection, javaobject

Tags: packagename
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
No problem, and I forgot to say it needs additional permissions, Thanks. I'll add it to the first post too.
 

bsnqt

Active Member
Licensed User
Longtime User
@stevel05: that is a great code snippet. Thank you for sharing... Now I learnt more about JavaObject from it.

I share another one, using OSLibrary (in fact I think they are very similar):

B4X:
    Private OS As OperatingSystem    : OS.Initialize("")
    Private RunList As List = OS.getRunningTasks(1)
    Private xt As String = "", r1 As Reflector
    If RunList.Size > 0 Then
        r1.Target = RunList.Get(0)
        xt = r1.getField("topActivity")
        Log(xt)
    End If
 
Top