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
Retrieve the RunningAppProcessInfo List to figure out if the desired app is running .
RunningAppProcessInfo
Try the following code , you need dependency service to consume the method implemented in Android project .
public interface IHelper { bool IsAppRunning(string packageName); } bool isRunning = DependencyService.Get<IHelper>().IsAppRunning("com.your.desired.app");
[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.
Answers
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
Android project
Refer to
https://stackoverflow.com/a/4213851/8187800.