The examples in this article share with you the specific implementation code for Android to determine the current App status for your reference. The specific content is as follows
The first type:
/** *Discern whether the current application is in the foreground or the background * * @param context * @return */ public static boolean isApplicationBroughtToBackground(final Context context) { ActivityManager am = (ActivityManager) (Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = (1); if (!()) { ComponentName topActivity = (0).topActivity; if (!().equals(())) { return true; } } return false; }
The above code requires a permission:
< uses-permission Android:name=”.GET_TASKS” />
The second type:
The following code is my latest discovery, without permission, it is absolutely true:
However, I made a little adjustment and the background is divided into: service background and activity background. Everyone who thinks that it is not the front desk thinks it is the background.
•service is running in the background, = IMPORTANCE_SERVICE,
•There is no background for service running,
public static boolean isBackground(Context context) { ActivityManager activityManager = (ActivityManager) (Context.ACTIVITY_SERVICE); List<> appProcesses = (); for ( appProcess : appProcesses) { if ((())) { if ( == .IMPORTANCE_FOREGROUND) { (("Foreground App:", )); return false; }else{ ("Background App:"+); return true; } } } return false; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.