SDK2.3适用的判断当前是否Launcher的方法
2016-11-28 20:33
344 查看
记录一下,如何判断当前activity是否launcher的准确的办法?
错误方式:getRunningTasks ---
private boolean isHome() {ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfoList = mActivityManager.getRunningTasks(1);
if (taskInfoList == null || taskInfoList.isEmpty()) {
return false;
}
return getHomes().contains(taskInfoList.get(0).topActivity.getPackageName());
}
正确方式:getRunningAppProcesses ---
public boolean isHome() {ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return getHomes().contains(appProcess.processName);
}else{
return true;
}
}
return false;
}
公共部分代码:
private List<String> getHomes() {
List<String> names = new ArrayList<String>();
PackageManager packageManager = mContext.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) {
if ("com.kitsmart.setupwizard".equals(ri.activityInfo.packageName))
continue;
names.add(ri.activityInfo.packageName);
}
return names;
}
相关文章推荐
- 判断当前Event触点是否在指定区域之内的两种方法
- Android 用ping的方法判断当前网络是否可用
- Android 判断当前线程是否是主线程的两种方法
- 判断当前页面是否在微信浏览器中打开(四类方法整理)
- Android 判断当前的界面是否是桌面的方法
- 判断当前进程是否以管理员程序运行的方法
- Android 判断当前线程是否是主线程的两种方法
- 这是个用来确认当前是否是调试模式然后判断是否需要打印日志的很好的方法,具体链接不知道,但是很感谢作者
- Android编程判断当前指定App是否在前台的方法
- 判断当前window对象是否为顶级窗口的两种方法
- Android 判断当前线程是否是主线程的两种方法
- Android判断当前线程是否是主线程的方法
- Android 判断当前线程是否是主线程的两种方法
- JS判断当前页面是否在微信浏览器打开的方法
- MFC的View子类中判断当前view是否是活动的方法
- Android 判断当前线程是否是主线程的方法
- Android 判断当前的界面是否是桌面的方法
- Android--用ping的方法判断当前网络是否可用
- Android判断当前线程是否是主线程的方法
- 如何判断当前的Fragment是否可见?Fragment的可见与不可见的判断方法。