Forum Visual Studio
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Find Which program is running

hi

I want to know which app is running in Xamarin.Forms. If the app I want was open, run a Method

Please help me

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 26

    Retrieve the RunningAppProcessInfo List to figure out if the desired app is running .

    Try the following code , you need dependency service to consume the method implemented in Android project .

    Forms project

      public interface IHelper
        {
            bool IsAppRunning(string packageName);
        }
    
    
     bool isRunning = DependencyService.Get<IHelper>().IsAppRunning("com.your.desired.app");
    

    Android project

    [assembly: Dependency(typeof(FormsApp.Droid.Helper))]
    namespace FormsApp.Droid
    {
        class Helper : IHelper
        {
            public bool IsAppRunning(string packageName)
            {
                ActivityManager activityManager = (ActivityManager)Application.Context.GetSystemService(Context.ActivityService);
                IList<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.RunningAppProcesses;
    
                if(procInfos != null)
                {
                    foreach (var processInfo in procInfos)
                    {
                        if (processInfo.ProcessName.Equals(packageName))
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
    }
    

    Refer to

    https://stackoverflow.com/a/4213851/8187800.

Sign In or Register to comment.