Search code examples
c#xamarin.net-6.0mauimaui-android

Getting the list of running processes in Android OS using Net Maui


I'm trying the access the list of Processes that are running on Android OS ( I'm talking about API 27 ), using NET MAUI (NET 6) but now the list returns null. I don't know how am I supposed to access this.

I tried on Xamarin using the below method to get the list of running processes and worked as intended.

ActivityManager activityManager = mauicontext.GetSystemService(Context.ActivityService) as ActivityManager;
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.RunningAppProcesses as List<ActivityManager.RunningAppProcessInfo>;

Now using Net Maui (Net.6) the same method the runningAppProcesses List is returning null.


Solution

  • At first, did you add the following permission in the AndroidManifest.xml?

    mlns:tools="http://schemas.android.com/tools"
    
    <uses-permission android:name="android.permission.REAL_GET_TASKS"
        tools:ignore="ProtectedPermissions" />
    

    In addition, since the Android 7.0, the activityManager.RunningAppProcesses can only get your own app. But you can use the following code to try to get the current runing app in the Android 8.1.

     PackageManager localPackageManager = this.PackageManager;
     var localList = localPackageManager.GetInstalledPackages(0).ToList();
     var app = new Java.Util.ArrayList();
     for (int i = 0; i < localList.Count; i++)
    {
         PackageInfo localPackageInfo1 = (PackageInfo)localList[i];
         String str1 = localPackageInfo1.PackageName.Split(":")[0];
         if (((ApplicationInfoFlags.System & localPackageInfo1.ApplicationInfo.Flags) == 0)
          && ((ApplicationInfoFlags.UpdatedSystemApp & localPackageInfo1.ApplicationInfo.Flags) == 0)
          && ((ApplicationInfoFlags.Stopped & localPackageInfo1.ApplicationInfo.Flags) == 0))
           {
              app.Add(str1);
           }
     }