Determine if the app is in the foreground or backstage

Source: Internet
Author: User

转载请注明转自:[noyet12的博客](http://blog.csdn.net/u012975705) 博客原址:http://blog.csdn.net/u012975705/article/details/50109883

The previous blog said, using notification in the project, the effect is to pull the background data when the app exits the foreground, and discover that the new dynamic will prompt the user in the notification bar. To achieve this effect, it is necessary to determine whether the current app is in the foreground work, the following is a way to determine.

     Public Boolean Isapponforeground() {Activitymanager Activitymanager = (Activitymanager) getapplicationcontext (). Getsystemservice (C Ontext.        Activity_service);        String PackageName =getapplicationcontext (). Getpackagename (); list<activitymanager.runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses ();if(Appprocesses = =NULL) {return false; } for(Activitymanager.runningappprocessinfo appprocess:appprocesses) {if(AppProcess.processName.equals (packagename) && appprocess.importance = = activitymanager.running Appprocessinfo.importance_foreground) {return true; }        }return false; }

How to use:

 @Override    protected void OnStop() {Super. OnStop ();if(Mainapplication.getinstance (). Getislogin () &&! Mainapplication.getinstance (). Isapponforeground ()) {Mtask =NewTimerTask () {@Override                 Public void Run() {getnews ();            }            }; Mtimer =NewTimer (); Mtimer.schedule (Mtask,3* +,3* -* +); }    }

Determine if the app is in the foreground or backstage

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.