处理检测app进去后台的时候提示用户 也就是在onPause的时候
/**
* Is foreground boolean.
*
* @param context the context
* @return the boolean
*/
/*判断应用是否在前台*/
public static boolean isForeground(Context context) {
try {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
assert am != null;
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
} catch (SecurityException e) {
e.printStackTrace();
return false;
}
}
if (!Selfutils.isForeground(this)) {
setShow(Config.uiStyle + "应用仍在后台运行,如需退出,请先进入"+Config.uiStyle+"应用,按手机“返回键”退出。");
} |