Android擷取Activity(應用)的運行狀態及其他資訊

來源:互聯網
上載者:User

標籤:


檢測某Activity是否在當前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;    }


Android擷取Activity(應用)的運行狀態及其他資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.