转载请注明转自:[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