zoukankan      html  css  js  c++  java
  • 获取Android正在运行的任务和服务

    检测某ActivityUpdate是否在当前Task的栈顶

    public static boolean isTopActivy(String cmdName, Context context)
        {
            ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(Integer.MAX_VALUE);
            String cmpNameTemp = null;
            if (null != runningTaskInfos)
            {
                cmpNameTemp = (runningTaskInfos.get(0).topActivity).toString();
            }
    
            if (null == cmpNameTemp)
            {
                return false;
            }
            
            return cmpNameTemp.equals(cmdName);
    
        }

    判断Android应用是否在前台

    public static boolean isAppOnForeground(Context context)
        {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            String packageName = context.getPackageName();
            List<RecentTaskInfo> appTask = activityManager.getRecentTasks(Integer.MAX_VALUE, 1);
            
            if (appTask == null)
            {
                return false;
            }
            
            if (appTask.get(0).baseIntent.toString().contains(packageName))
            {
                return true;
            }
            return false;
        }

    获取Android手机内安装的所有桌面

    private static List<String> getAllTheLauncher(Context context)
        {
            List<String> names = null;
            PackageManager pkgMgt = context.getPackageManager();
            Intent it = new Intent(Intent.ACTION_MAIN);
            it.addCategory(Intent.CATEGORY_HOME);
            List<ResolveInfo> ra = pkgMgt.queryIntentActivities(it, 0);
            if (ra.size() != 0)
            {
                names = new ArrayList<String>();
            }
            for (int i = 0; i < ra.size(); i++)
            {
                String packageName = ra.get(i).activityInfo.packageName;
                names.add(packageName);
            }
            return names;
        }

    Android 判断程序前后台状态

    public static boolean isLauncherRunnig(Context context)
        {
            boolean result = false;
            List<String> names = getAllTheLauncher(context);
            ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses();
            for (RunningAppProcessInfo running : appList)
            {
                if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
                {
                    for (int i = 0; i < names.size(); i++)
                    {
                        if (names.get(i).equals(running.processName))
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            return result;
        }
  • 相关阅读:
    10分钟带你入门git到github
    ArrayList的删除姿势你都知道了吗
    大白话Java多线程,小白都能看的懂的哦
    万恶的NPE差点让我半个月工资没了
    宇宙条面试:这样跟面试官说代理,面试官对我竖起了大拇指!
    在项目中随手把haseMap改成了currenHaseMap差点被公司给开除了。
    java采坑之路
    本地缓存高性能之王Caffeine
    本地缓存性能之王Caffeine
    influxdb基本命令
  • 原文地址:https://www.cnblogs.com/shaweng/p/2575308.html
Copyright © 2011-2022 走看看