首页 > 代码库 > 判断android应用是否在运行等方式
判断android应用是否在运行等方式
/*** * 判断应用当前是否前台运行 * * @return */ public static boolean isTopRun(Context context) { Boolean isTopRun = false; try { /* 判断当前程序是不在前台显示 */ ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager .getRunningTasks(1); if (tasksInfo.size() > 0) { String packageName = tasksInfo.get(0).topActivity .getPackageName(); if (!TextUtils.isEmpty(packageName) && packageName.startsWith(getAppPackageName())) { isTopRun = true; } } } catch (Exception e) { e.printStackTrace(); } return isTopRun; }
/*** * 判断应用是否在运行 * * @return */ public static boolean isAppRunning(Context context){ boolean isAppRunning = false; try { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> list = activityManager.getRunningTasks(100); String pageName = "com.example.packagename"; pageName = getAppPackageName(); for (RunningTaskInfo info : list) { if (info.topActivity.getPackageName().equals(pageName) || info.baseActivity.getPackageName().equals(pageName)) { isAppRunning = true; Log.i("SystemUtils",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName()); break; } } } catch (Exception e) { e.printStackTrace(); } return isAppRunning; }
/*** * 判断应用是否在运行 * * @return */ public static boolean isAppRunning(Context context){ boolean isAppRunning = false; try { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> list = activityManager.getRunningTasks(100); String pageName = "com.example.packagename"; pageName = getAppPackageName(); for (RunningTaskInfo info : list) { if (info.topActivity.getPackageName().equals(pageName) || info.baseActivity.getPackageName().equals(pageName)) { isAppRunning = true; Log.i("SystemUtils",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName()); break; } } } catch (Exception e) { e.printStackTrace(); } return isAppRunning; }
判断android应用是否在运行等方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。