Android obtains the running status and other information of an Activity (application ).

Source: Internet
Author: User

Android obtains the running status and other information of an Activity (application ).

Checks whether an Activity is on the top of the current Task stack.

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);    }
 

 

Determine whether the Android Application is on the frontend
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;    }
 

 

Obtain all desktops installed on your Android phone
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 determines the frontend and backend status of a program
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;    }
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.