要检查应用程序是否在foregraound或background中,我正在尝试按以下方式访问ActivityManager。
ActivityManager activityManager = GetSystemService(Context.ACTIVITY_SERVICE) as ActivityManager;
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
foreach(RunningAppProcessInfo appProcess in appProcesses){
if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
Log.i("Foreground App", appProcess.processName);
//SetNotification ();
}
}
但是我得到了恭维错误,因为“Android.Content.Context在第一行没有包含ACTIVITY_SERVICE的定义”。
如何在Xamarin中访问ActivityManager?
答案 0 :(得分:5)
从
改变ActivityManager activityManager = GetSystemService(Context.ACTIVITY_SERVICE) as ActivityManager;
到
ActivityManager activityManager = GetSystemService(Context.ActivityService) as ActivityManager;
获取ActivityManager
的实例。