This method is encapsulated in my app that needs to see my app and determine if an activity is running on the phone interface
PackageName is a package name that needs to be judged to be applied, such as Com.xy.tencent
It is also possible to determine whether an activity is displayed on the current phone, or the complete path is written anyway!
<span style= "FONT-SIZE:18PX;" >/** * Judging the operation of the program in the foreground or backstage * * @param context * @return 0 running in the background greater than 0 in the foreground running 2 indicates that the current main interface is mainfragmentactivity */public static int IsBackground (Context context) {Activitymanager Activitymanager = (activitymanager) context.getsystemservice ( Context.activity_service); String PackageName = "Com.xy.tencent"; String bingmapmainactivityclassname = "com.xy.tencent.activity.MainFragmentActivity"; list<runningtaskinfo> tasksinfo = activitymanager.getrunningtasks (1); if (tasksinfo.size () > 0) { ComponentName topconponent = tasksinfo.get (0). topactivity; LOGUTIL.D ("TAG", "Topconponent.getpackagename () ..." + topconponent.getpackagename ()); if (Packagename.equals ( Topconponent.getpackagename ())) {//The current app runs in the foreground if (Topconponent.getclassname (). Equals ( Bingmapmainactivityclassname) {//is currently running is not expected ACTIVITYLOGUTIL.D ("TAG", "Mainfragmentactivity is Running"); return 2;} LOGUTIL.D ("TAG", "com.xy.tencent foreground operation"); return 1;} else {//the current app runs in the background logutil.d ("TAG", "Com.xy.tencent background Run"); return 0;}} RetUrn 0;} </span>
Android to determine if an app or activity is running