Android determines whether an application or activity is running. androidactivity
This method is encapsulated in my applications and needs to check my applications and determine whether an activity is running on the mobile phone interface.
PackageName indicates the application package name, such as com. xy. tencent.
You can also determine whether an activity is displayed on the current mobile phone. You can write the complete path!
<Span style = "font-size: 18px; ">/*** determine whether the program runs on the frontend or backend ** @ param context * @ return 0: if the program runs at the backend, it runs at the frontend. If the value is greater than 0, 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 if (topConponent. getClassName (). equals (bingMapMainActivityClassName) {// whether the currently running ActivityLogUtil is expected. d ("TAG", "MainFragmentActivity is running"); return 2;} LogUtil. d ("TAG", "com. xy. run "); return 1;} else {// The current APP runs LogUtil in the background. d ("TAG", "com. xy. tencent background running "); return 0 ;}return 0 ;}</span>