/*
 * 判断一个APP 是否在前台,还是在后台
 */
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<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null)
        {
            return false;
        }
        for (RunningAppProcessInfo appProcess : appProcesses)
        {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
            {
                return true;
            }
        }
        return false;
    }

 PS:此方法是网上搜索来的!

相关文章:

  • 2022-12-23
  • 2021-07-21
  • 2021-05-15
  • 2021-09-29
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-29
  • 2022-12-23
  • 2022-03-05
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
相关资源
相似解决方案