1.判断是否运行
<uses-permission android:name="android.permission.GET_TASKS" />
static boolean isAppRunning(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> list = null; if (activityManager != null) { list = activityManager.getRunningTasks(100); } if (list == null || list.size() <= 0) { return false; } for (ActivityManager.RunningTaskInfo info : list) { if (info.baseActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; }
2.判断是否在前台
private static boolean isAppForeground(Context context) { if (context != null) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> processes; if (activityManager != null) { processes = activityManager.getRunningAppProcesses(); } else { return false; } for (ActivityManager.RunningAppProcessInfo processInfo : processes) { if (processInfo.processName.equals(context.getPackageName())) { MLog.i(FaceDetectionApp.TAG, "processInfo.importance = " + processInfo.importance); if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } } } return false; }
3.判断是否在后台
通过1和2 可以判断后台
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦