就简单的一个方法,就不用写什么步骤了:
public static boolean Frontdesk(Context context) { ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); ListappProcesses = activityManager .getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName())) { Log.i(context.getPackageName(), "此appimportace =" + appProcess.importance + ",context.getClass().getName()=" + context.getClass().getName()); if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { Log.i(context.getPackageName(), "处于后台" + appProcess.processName); return true; } else { Log.i(context.getPackageName(), "处于前台" + appProcess.processName); return false; } } } return false; }
在需要的地方调用Frontdesk(context)方法即可
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!