有时候我们再做一个程序的时候需要根据当前焦点是不是在桌面来做相应的处理
/**get the launcher status */ private boolean isLauncherRunnig(Context context) { boolean result = false ; List<String> names = getAllTheLauncher(); ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ; List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses() ; for (RunningAppProcessInfo running : appList) { if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { for (int i = 0; i < names.size(); i++) { if (names.get(i).equals(running.processName)) { result = true ; break; } } } } return result ; }
其中getAllLauncher()方法如下:
private List<String> getAllTheLauncher(){ List<String> names = null; PackageManager pkgMgt = this.getPackageManager(); Intent it = new Intent(Intent.ACTION_MAIN); it.addCategory(Intent.CATEGORY_HOME); List<ResolveInfo> ra =pkgMgt.queryIntentActivities(it,0); if(ra.size() != 0){ names = new ArrayList<String>(); } for(int i=0;i< ra.size();i++) { String packageName = ra.get(i).activityInfo.packageName; names.add(packageName); } return names; }