如何在Xamarin中获取ActivityManager的实例?

时间:2015-05-05 04:06:21

标签: android xamarin xamarin.android

要检查应用程序是否在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?

1 个答案:

答案 0 :(得分:5)

改变
ActivityManager activityManager = GetSystemService(Context.ACTIVITY_SERVICE) as ActivityManager;

ActivityManager activityManager = GetSystemService(Context.ActivityService) as ActivityManager;

获取ActivityManager的实例。