public class BaseActivity extends Activity{
@Overrideprotected void onStop() { boolean isOnForeground=isAppOnForeground(); RaiingLog.d("前后台测试-->>onStop-->>" + isAppOnForeground()+", name-->>"+this.getClass().getName()+", isShowDialog-->>"+isShowDialog); super.onStop();} @Overrideprotected void onResume() { super.onResume(); boolean isOnForeground=isAppOnForeground(); RaiingLog.d("前后台测试-->>onResume-->>" + isAppOnForeground()+", name-->>"+this.getClass().getName()+", isShowDialog-->>"+isShowDialog); }/** * 程序是否在前台运行 * * @return */public boolean isAppOnForeground() { // Returns a list of application processes that are running on the // device ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); String packageName = getApplicationContext().getPackageName(); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager .getRunningAppProcesses(); if (appProcesses == null) return false; for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { // The name of the process that this object is associated with. if (appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false;}}
时间: 2024-09-29 07:52:45