This is a small Service Module:
Service Name: Mysvc
Description: Interrogates the activity manager to find out the currently running foreground application.
Depends on: Reflection and JavaObject
Add this to your manifest:
Usage in Main:
Tags: Activity Manager Current application
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: