Android to determine if an app or activity is running

Source: Internet
Author: User


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

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.