android-判断app处于前台后台

调用此方法:


//当前应用是否处于前台

private boolean isForeground(Context context) {

if (context !=null) {

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

      ComponentName cn = am.getRunningTasks(1).get(0).topActivity;

      String currentPackageName = cn.getPackageName();

      if (!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) {

return true;

      }

return false;

  }

return false;

}

你可能感兴趣的:(android-判断app处于前台后台)