Two ways to determine if an app is running in the foreground

Source: Internet
Author: User

Often in this situation, the app receives new messages, and if the app is not in the foreground, send a notification alert in the phone notification bar. So, how to tell if the app is in the foreground


Check whether the current app is running in the foreground, not minimized, surf the web for a solution, and see a piece of code that can be implemented:

public Boolean Isrunningforeground (context context) {Activitymanager am = (activitymanager) context.getsystemservice ( Context.activity_service); ComponentName cn = Am.getrunningtasks (1). Get (0). topactivity; String currentpackagename = Cn.getpackagename (); if (currentpackagename! = null && currentpackagename.equals ( Getpackagename ())) {return true;} return false;}

It also uses a small technique to verify that the app is running in the foreground without using other special APIs, mainly using the activity's lifecycle callback method. First define a variable in your own defined application class to indicate whether the app is in the foreground

public class App extends Application{private boolean ifappear;//the app disappear into backgroundpublic void Appdisappear ( {ifappear = true;} The App appear onto Foregroundpublic void Appappear () {ifappear = false;} If the app in to Foregroundpublic Boolean ifappear () {return ifappear;}}

In the project, we often define a baseactivity (the other activity is inherited from it, baseactivity all the functions shared by the activity, can look at the following code fragment), according to the activity life cycle of knowledge, you can know, An activity is displayed on the screen, calls Onresume (), the activity disappears on the screen, the OnPause () is called, so the value of the above ifappear is modified in the two callback methods. Although multiple activity repeats the two methods of calling Baseactivity, there is no conflict, as explained below:

If an activity disappears (called OnPause () in baseactivity), Ifappear is set to False if the app has a new activity start, The ifappear becomes true (the new activity calls the Onresume in baseactivity), which means the app is still in the foreground, and it's true.

If an activity disappears (called OnPause () in baseactivity), Ifappear is set to false, and if the app does not have a new activity start, go back to the phone home screen, There is no new activity to invoke the Onresume () in the parent class baseactivity, so Ifappear continues to remain false, representing the entire app disappearing into the background, as it is.


The code for Baseactivity is as follows:

public class Baseactivity extends fragmentactivity{protected app, @Overrideprotected void OnCreate (Bundle bundle) {// TODO auto-generated Method Stubsuper.oncreate (bundle), if (app = = null) app = (APP) getapplication ();} @Overrideprotected void OnPause () {//TODO auto-generated method Stubsuper.onpause (); App.appappear ();} @Overrideprotected void Onresume () {//TODO auto-generated method Stubsuper.onresume (); App.appappear ();} @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy ();}}

You may wonder why you are not setting Ifappear to False in the OnStop () method, for the following reasons, OnPause () activitya start Activityb,a must be executed before the Onresume () of B, and A's OnStop () May be executed after the Onresume () of B. Refer to the official website documentation for the last paragraph of the Activity lifecycle:




This is when you build a framework for one of your own Android XMPP instant messaging tools, adding a little trick, added to the code, but not really used, and will be used to receive chat messages later. The project built a frame and wrote the landing function. Try to use a lot of knowledge of Android in this project.

GitHub Address: GitHub address

Two ways to determine if an app is running in the foreground

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.