detects whether an activity is at the top of the current task's stack
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); }
determine if the Android app is in the foreground
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; }
get all the desktops installed in your Android phone
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 Judging program front and back status
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 gets activity (app) status and other information