Android判断当前界面是否为桌面
2017-08-31 11:12
393 查看
获取当前界面是否为桌面,需要用到
大致意思就是指,自从Android21之后该方法只能获取到自己应用的信息,获取不到其他包信息了,如果我们只需要判断我们自己的应用是否处于桌面,进行相应逻辑的处理是可以的。如果需要监听home键可以查看我的上篇Home监听。
照例,看一下判断桌面代码:
ok!!!
ActivityManager.getRunningTasks(1)这个方法,首先看一下这个方法的官方解释。
/** *@deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method * is no longer available to third party * applications: the introduction of document-centric recents means * it can leak person information to the caller. For backwards compatibility, * it will still retu rn a small subset of its data: at least the caller's * own tasks, and possibly some other tasks * such as home that are known to not be sensitive.
大致意思就是指,自从Android21之后该方法只能获取到自己应用的信息,获取不到其他包信息了,如果我们只需要判断我们自己的应用是否处于桌面,进行相应逻辑的处理是可以的。如果需要监听home键可以查看我的上篇Home监听。
照例,看一下判断桌面代码:
public static boolean isHomes() { try { ActivityManager mActivityManager = (ActivityManager)CallBlockerApplication.getInstance() .getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> rti = mActivityManager.getRunningTasks(1); List<String> strs = getHomes(); if(strs != null && strs.size() > 0 && rti != null && rti.size() > 0){ return strs.contains(rti.get(0).topActivity.getPackageName()); }else{ return false; } } catch (SecurityException e) { e.printStackTrace(); return false; } } /** * 获得属于桌面的应用的应用包名称 * @return 返回包含所有包名的字符串列表 */ public static List<String> getHomes() { List<String> names = new ArrayList<String>(); try { PackageManager packageManager = CallBlockerApplication.getInstance().getPackageManager(); //属性 Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); for(ResolveInfo ri : resolveInfo){ names.add(ri.activityInfo.packageName); } return names; } catch (Exception e) { e.printStackTrace(); return names; } }
ok!!!
相关文章推荐
- android 小技巧之 --- 判断当前界面是否是桌面
- android判断当前界面是否是桌面(主页)
- android判断当前界面是否是桌面(主页)
- Android判断当前界面是否是桌面
- Android 判断当前的界面是否是桌面的方法
- Android判断当前界面是否是桌面(主页)
- [Android]判断当前界面是否是桌面
- android 判断当前界面是否是桌面
- 【转】android判断当前界面是否是桌面(主页)
- Android 判断当前的界面是否是桌面的方法
- Android判断当前界面是否为在桌面
- Android中 如何判断现在所处的界面是否为home主桌面
- Android判断当前界面是否在launcher,及获取应用包名入口Activity
- android 判断是否在桌面,当前程序是否在运行
- Android判断现在所处界面是否为home主桌面的方法
- Android----获得当前应用包名、当前activity的名字、判断是否是系统桌面、service类
- Android中 如何判断现在所处的界面是否为home主桌面
- 判断当前界面是否是桌面
- Android中 如何判断现在所处的界面是否为home主桌面
- Android判断当前应用通知是否 4000 打开,跳转到打开通知界面