List of Running Applications and Package Names

treehousefrog

Member
Licensed User
Longtime User
Hey guys,

I'm trying to create a task-switcher type function for an app I'm developing that will show currently running applications then launch them with an intent at a click. I tried using the OSLibrary but I've really struggled to get the package name from the lists using reflection. I was thinking there should probably be an easier way?

So is there an easy way to do this? Just get the currently running app package names and put them in a list? If not, could someone please help me out with OSLibrary?

Thanks a lot in advance, I've spent literally all day trying solutions. Headache!
 

treehousefrog

Member
Licensed User
Longtime User
Thanks man, but how do I go about finding the relevant package based on the information I get from the OSLibrary? I need to get the com.package.this of the most recent applications...

In other words, I can use OSLibrary to get a things like processes, but with errors, and can't really load them. With PackageManager I can get labels if I have intents and launch apps, but I don't know which apps to launch...

I hope that makes sense...

This is my broken attempt at loading the most recent app (I know it's very broken...):

Dim whichapp As String
Dim o As OperatingSystem
Dim tasks As List = o.getRunningTasks(1)
'For Each task As Object In tasks
Dim task As Object
Dim r As Reflector
task = tasks.Get(0)
r.Target = task
Dim bi As Object = r.GetField("baseIntent")
r.Target = bi
r.Target = r.RunMethod("getComponent")
whichapp = r.RunMethod("getPackageName")
Dim pack As PackageManager

ToastMessageShow(whichapp, True)
Try
Dim pm As PackageManager
Dim il As Intent
il = pm.GetApplicationIntent (whichapp)
swirlmenu.Left = 9000
allapps.Left = 9000
transparent = 1
backg.initialize(File.DirAssets, "trans.png")
Activity.SetBackgroundImage(backg)
StartActivity (il)
Catch
ToastMessageShow ("Failed to launch app! " & whichapp & " Is it installed?", True)
End Try


Thanks again!
 
Upvote 0

treehousefrog

Member
Licensed User
Longtime User
So I figured it out with some research and trial and error! For anyone else who wants to do this...


Dim o As OperatingSystem
Dim tasks As List = o.getRunningTasks(10)
'For Each task As Object In tasks
Dim task As Object
Dim whichapp As String
task = tasks.Get(1)
Dim r As Reflector
r.Target = task
Dim bi As Object = r.GetField("baseActivity")
If bi <> Null Then
r.Target = bi
'r.Target = r.RunMethod("getComponent")
whichapp = r.RunMethod("getPackageName")
Msgbox(whichapp, "Test")
End If


And then just launch the app with an intent. Of course you could use get(2) if you wanted to launch something older, or you could show the list in a listview. Hopefully this helps someone out there!
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I'll post this as you might find it useful:

ActivityManager
Version: 0.01
  • ActivityManager
    Methods:
    • GetLaunchIntentForLauncher As IntentWrapper
      Returns an Intent that will launch the device's default launcher/home screen application.
      Starting this Intent will force any running application into the background.
    • GetLaunchIntentForPackage (PackageName As String) As IntentWrapper
      Returns an Intent that will start/restart the application identified by PackageName.
      The Intent will ensure that the application is started afresh with no 'back' history.
    • GetRunningTasks As RunningTaskInfo[]
      Returns an Array of RunningTaskInfo objects.
      Each RunningTaskInfo object represents a task that the user has recently launched.
    • KillBackgroundProcesses (PackageName As String)
      Have the system immediately kill all background processes associated with the given package name.
    Permissions:
    • android.permission.GET_TASKS
    • android.permission.KILL_BACKGROUND_PROCESSES
  • RunningTaskInfo
    Methods:
    • GetApplicationName As String
      Returns this running task's application name.
      Will return "APPLICATION_NAME_NOT_FOUND" if a problem occurs trying to find the application name.
    • GetPackageName As String
      Returns this running task's package name.
    • IsInitialized As Boolean

It's part of a library i developed for someone else and here i have extracted just the ActivityManager and RunningTaskInfo objects.
We found the KillBackgroundProcesses method sometimes unreliable and unpredictable though so resorted to running adb commands using the shell to kill a process.

Hopefully you can work out how to use it - if not i'll put some example code together when i have a spare moment (busy right now).

Martin.
 

Attachments

  • ActivityManager_20130531.zip
    5.2 KB · Views: 1,106
Upvote 0

Mossy69b4a

New Member
Licensed User
Longtime User
applications names

Please can some help

id like to liste the application names correcly like on my tablette does for exemple:

Navigation with the icon (blue arrow pointing upwards)

this is my code for the moment

B4X:
Dim pm As PackageManager
Dim Icons As BitmapDrawable
Dim Appname As String


Dim Obj1, Obj2, Obj3 As Reflector
Dim size, i, flags As Int
Dim msg, name As String
Obj1.Target = Obj1.GetContext
Obj1.Target = Obj1.RunMethod("getPackageManager") ' PackageManager
Obj1.Target = Obj1.RunMethod2("getInstalledPackages", 0, "java.lang.int") ' List<PackageInfo>
size = Obj1.RunMethod("size")
For i = 0 To size -1
    Obj2.Target = Obj1.RunMethod2("get", i, "java.lang.int") ' PackageInfo
    name = Obj2.GetField("packageName")
    Obj3.Target = Obj2.GetField("applicationInfo") ' ApplicationInfo        
      
  
   
   
    Icons = pm.GetApplicationIcon(name)
    Appname = pm.GetApplicationLabel (name)
   
   AppList.AddTwoLinesAndBitmap ( Appname,name,Icons.Bitmap )

the application Navigation i'snt even in the list


please Help :sign0085:
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
I'll post this as you might find it useful:

ActivityManager
Version:
0.01
  • ActivityManager
    Methods:
    • GetLaunchIntentForLauncher As IntentWrapper
      Returns an Intent that will launch the device's default launcher/home screen application.
      Starting this Intent will force any running application into the background.
    • GetLaunchIntentForPackage (PackageName As String) As IntentWrapper
      Returns an Intent that will start/restart the application identified by PackageName.
      The Intent will ensure that the application is started afresh with no 'back' history.
    • GetRunningTasks As RunningTaskInfo[]
      Returns an Array of RunningTaskInfo objects.
      Each RunningTaskInfo object represents a task that the user has recently launched.
    • KillBackgroundProcesses (PackageName As String)
      Have the system immediately kill all background processes associated with the given package name.
    Permissions:
    • android.permission.GET_TASKS
    • android.permission.KILL_BACKGROUND_PROCESSES
  • RunningTaskInfo
    Methods:
    • GetApplicationName As String
      Returns this running task's application name.
      Will return "APPLICATION_NAME_NOT_FOUND" if a problem occurs trying to find the application name.
    • GetPackageName As String
      Returns this running task's package name.
    • IsInitialized As Boolean

It's part of a library i developed for someone else and here i have extracted just the ActivityManager and RunningTaskInfo objects.
We found the KillBackgroundProcesses method sometimes unreliable and unpredictable though so resorted to running adb commands using the shell to kill a process.

Hopefully you can work out how to use it - if not i'll put some example code together when i have a spare moment (busy right now).

Martin.

thank for the library
i'm getting not intlized error when i'm trying to do this:
B4X:
task_info.GetPackageName
any ideas ?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
task_info is an instance of a RunningTaskInfo object is it?
Where did you get the instance from - from the ActivityManager GetRunningTasks method?

Can you post the code you're using?
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
task_info is an instance of a RunningTaskInfo object is it?
Where did you get the instance from - from the ActivityManager GetRunningTasks method?

Can you post the code you're using?


B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim task_info As RunningTaskInfo

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Log(task_info.GetApplicationName)
End Sub


** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 29 (Main)
java.lang.RuntimeException: Object should first be initialized (RunningTaskInfo).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at uk.co.martinpearman.b4a.activitymanager.RunningTaskInfo.GetApplicationName(RunningTaskInfo.java:28)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:668)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:334)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:244)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
at b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Try something like this:

B4X:
Dim ActivityManager1 As ActivityManager

Dim RunningTaskInfos() As RunningTaskInfo
RunningTaskInfos=ActivityManager1.GetRunningTasks
Log("RunningTaskInfos.Length="&RunningTaskInfos.Length)

For Each RunningTaskInfo1 As RunningTaskInfo In RunningTaskInfos
    Log(RunningTaskInfo1.GetApplicationName)
Next

This code gets an array of initialized RunningTaskInfo objects from the ActivityManager.
 
Upvote 0
Top