Determine whether the app is running on the frontend and whether the app is running on the frontend.

Source: Internet
Author: User

Determine whether the app is running on the frontend and whether the app is running on the frontend.

In this situation, the app receives new messages. If the app is not in the foreground, a notification is sent in the notification bar of the mobile phone. How can I determine whether the app is on the frontend?


Check whether the current app is running on the front-end without being minimized. Check the solution on the Internet and see that a piece of code 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;}

I also used a small trick to check whether the app is running on the frontend. I don't need to use other special APIs. I mainly use the activity lifecycle callback method. First, define a variable in your own 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 projects, we often define a BaseActivity (Other Activities inherit from it, and BaseActivity includes all the functions of the Activity. You can see the code snippets below ), based on the knowledge of the activity life cycle, you can know that when an activity is displayed on the screen, onResume () is called. When the activity disappears on the screen, onPause () is called (), therefore, modify the above ifAppear values in the two callback methods. Although multiple activities repeatedly call the two methods of BaseActivity, there is no conflict. The explanation is as follows:

If an activity disappears (onPause () in BaseActivity is called), ifAppear is set to false. If a new activity is started, ifappear is changed to true (the new activity calls onResume in BaseActivity), which means that the app is still at the front end, as is the case.

If an activity disappears (onPause () in BaseActivity is called), ifAppear is set to false. If no new activity is started, the app returns to the main screen of the mobile phone, at this time, no new activity calls onResume () in the parent class BaseActivity, So If ifAppear continues to be set to false, the whole app disappears to the background. This is also true.


The BaseActivity code is as follows:

public class BaseActivity extends FragmentActivity{protected App 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 ifAppear is not set to false in the onStop () method. The reason is as follows: ActivityA starts ActivityB, and onPause () of A will be executed before OnResume () of B, however, onStop () of A may be executed after onResume () of B. You can refer to the last section of the official website documentation about the activity lifecycle:




This is a small skill added to the code when setting up a framework for an android-side XMPP instant messaging tool. It is not actually used yet and will be used later when receiving chat information. The project has a framework and the login function is provided. Strive to use more android knowledge in this project.

Github address: github address

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.