Android determines whether an application or activity is running. androidactivity

Source: Internet
Author: User

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>


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.