1 /** 2 * 获得当前活动APP的包名 3 * 4 * @return 返回当前活动界面是不是桌面 5 */ 6 private boolean isHomeWin() 7 { 8 ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 9 List<RunningTaskInfo> lstRti = mActivityManager.getRunningTasks(1); 10 return getHomes().contains(lstRti.get(0).topActivity.getPackageName()); 11 } 12 13 14 /** 15 * 获得属于桌面的应用的应用包名称 16 * 17 * @return 返回包含所有包名的字符串列表 18 */ 19 private List<String> getHomes() { 20 List<String> packages = new ArrayList<String>(); 21 PackageManager packageManager = this.getPackageManager(); 22 Intent intent = new Intent(Intent.ACTION_MAIN); 23 intent.addCategory(Intent.CATEGORY_HOME); 24 List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY); 25 for (ResolveInfo ri : resolveInfo) { 26 packages.add(ri.activityInfo.packageName); 27 } 28 return packages; 29 }
在浮动窗口开发中,经常要用到该判断,如果在桌面的时候才显示浮动窗口。
android 判断当前界面是否是桌面
时间: 2024-11-05 22:00:20