我很惊讶!
由于无法确定当前在前台或后台应用程序,
Because not able identify application currently in foreground or background,
当我pressed home键和我的应用程序是不是在前台。再后,我已经习惯了检查正在运行的应用程序在前台还是不行。
when i pressed home button and my application is not in foreground. then after i have used to check application running in foreground or not.
public boolean isAppOnForeground() { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { return false; } final String packageName = context.getPackageName(); for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { return true; } } return false; }
它会总是返回真正即使我的应用程序实际上不会在前台。
it will returns always true even if my application actually not in foreground.
注意: 我的应用程序有2个服务, 当应用程序从最近的任务列表中删除1服务将被取消(,我取得的成绩。) 当应用程序进入后台2服务将被取消。 (即检查WiFi信号强度。)
Note: My application have 2 services, 1 service will be canceled when application is removed from recent task list( that i have achieved.) 2 service will be canceled when application goes in background. (that is check wifi signal strength.)
如何知道应用程序是前景或背景?
如何在应用程序是不是在前台?停止服务
推荐答案靠活动回调方法,如的onPause(),的onStop()和 onResume(),会给你提示的当前状态活动。该活动参考给每个这些方法的一个非常广泛的解释。
Rely on Activity callback methods, such as onPause(), onStop() and onResume(), that will give you hints on the current state of Activity. The Activity reference gives a very broad explanation of every of those methods.
更多推荐
检查应用程序最小化/背景
发布评论