• android判断当前应用程序处于前台还是后台


         /**
         *判断当前应用程序处于前台还是后台
         *
         * @param context

         * @return   
         */
        public static boolean isApplicationBroughtToBackground(final Context context) {
            ActivityManager am = (ActivityManager) context.getSystemService  

    (Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> tasks = am.getRunningTasks(1);
            if (!tasks.isEmpty()) {
                ComponentName topActivity = tasks.get(0).topActivity;
                if (!topActivity.getPackageName().equals(context.getPackageName())) {
                    return true;
                }
            }
            return false;

        }
    上面这段代码是需要一个权限的:
    <uses-permission android:name="android.permission.GET_TASKS" /> 
     
    下面这段代码也可以判断,无需权限
     /**
     *
     * @param context
     * @return
     */
     public static boolean isBackground(Context context) {

      ActivityManager activityManager = (ActivityManager) context
        .getSystemService(Context.ACTIVITY_SERVICE);
      List<RunningAppProcessInfo> appProcesses = activityManager
        .getRunningAppProcesses();
      for (RunningAppProcessInfo appProcess : appProcesses) {
       if (appProcess.processName.equals(context.getPackageName())) {
        if (appProcess.importance ==

    RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
         Log.i(String.format("Background App:",
           appProcess.processName));
         return true;
        } else {
         Log.i(String.format("Foreground App:",
           appProcess.processName));
         return false;
        }
       }
      }
      return false;
     }

  • 相关阅读:
    【转】进程与线程的一个简单解释
    折半查找
    BOJ 89 统计时间间隔
    BOJ 88 最值问题
    BOJ 87 日期
    QT杂记(网上资料整理而成)
    我的博客园
    【制作镜像】virsh
    【培训】MySQL
    ERROR 1045 (28000): Access denied for user 'root'@'localhost'
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3637702.html
Copyright © 2020-2023  润新知