Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android獲取Activity(應用)的運行狀態及其他信息

Android獲取Activity(應用)的運行狀態及其他信息

編輯:關於Android編程


檢測某Activity是否在當前Task的棧頂

public static boolean isTopActivy(String cmdName, Context context)
    {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List 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 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 getAllTheLauncher(Context context)
    {
        List names = null;
        PackageManager pkgMgt = context.getPackageManager();
        Intent it = new Intent(Intent.ACTION_MAIN);
        it.addCategory(Intent.CATEGORY_HOME);
        List ra = pkgMgt.queryIntentActivities(it, 0);
        if (ra.size() != 0)
        {
            names = new ArrayList();
        }
        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 names = getAllTheLauncher(context);
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List 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;
    }


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved