Android Judging program front and back status

Source: Internet
Author: User

public class Appstatusservice extends Service {private static final String TAG = "Appstatusservice";     Private Activitymanager Activitymanager;      Private String PackageName;     @Override public IBinder onbind (Intent Intent) {return null; } @Override public int onstartcommand (Intent Intent, int flags, int startid) {Activitymanager = (activit         Ymanager) This.getsystemservice (Context.activity_service);         PackageName = This.getpackagename ();                         New Thread () {public void run () {try {while (true) {                         Thread.Sleep (1000);                         if (Isapponforeground ()) {log.i (TAG, "true");                         } else {log.i (TAG, "false");                 }}} catch (Exception e) {e.printstacktrace (); }             }         }.Start ();     Return Super.onstartcommand (Intent, flags, Startid); } private Boolean Isapponforeground () {//Returns a list of application processes that is running on the de         vice List<runningappprocessinfo> appprocesses = Activitymanager.getrunningappprocesses ();         if (appprocesses = = null) return false; for (Runningappprocessinfo appprocess:appprocesses) {//importance://The relative importance             level, the system places//on this process. May is one of Importance_foreground, importance_visible,//Importance_service, Importance_background, or             Importance_empty. These constants is numbered so, "more important" values is//always smaller than "less important" va             Lues.             ProcessName://The name of the process, the this object was associated with.      if (AppProcess.processName.equals (PackageName)               && appprocess.importance = = runningappprocessinfo.importance_foreground) {return T             Rue     }} return false;  } }

Android Judging program front and back status

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.