Determine whether the App is running in the background and whether the App is running in the background

Source: Internet
Author: User

Determine whether the App is running in the background and whether the App is running in the background

 

In some scenarios, it is often necessary to determine whether the App is running in the background, such as whether to display the unlock interface and whether to display the Notification when a new message is received. Requirements may be diversified, but the principle is the same. Today Stay plans to talk about the optimal solution to these requirements.

Of course, Stay certainly won't say To Go To The for loop to judge the current runningProcess or runningTasks. For example:

This way

Or

Calling this method is like using a job manager in a Windows system. For the moment, we don't care about performance issues, so we can just say that Android doesn't even give me a decent API, which is really depressing.

If you investigate with such a question, you will find that there are other ways to implement it.

Android provides a Callback In SDK 14.ActivityLifecycleCallbacksYou can use this Callback to get the lifecycle Callback of all activities in the App. Figure:

This Callback is written in Application. You can register it during Application initialization. We can write a singleton class to cache these statuses. Here I call it AppStatusTracker. Register ActivityLifecycleCallbacks with AppStatusTracker in onCreate () of Application.

How can I know whether the App is running on the frontend.


Don't worry. Let's talk about the Activity lifecycle first. This is a required question during the interview. Although there are official answers, there is not much flexibility in understanding the life cycle.

We can imagine that if the Activity calls onResume (), the Activity must be visible, that is, running on the foreground. If onPause () is called and no Activity calls onResume (), the App will go to the background. I don't care whether the home or back key is clicked.

We can use the onActivityResumed () and onActivityPaused () Methods of the ActivityLifecycleCallbacks callback to count through this judgment. If there is only one activityCount, the current App is on the frontend. If the wood has activityCount, it is in the background.

Well, it's a pleasant solution, and we don't need to use for loop anymore. But soon you will find that there is a delay, which will lead to inaccurate judgment.

Let's assume there are two Activities, one is A and one is B. How does one take the lifecycle from A to B? A. onPause ()-> B. onResume () corresponds to ActivityLifecycleCallbacks, Which is onActivityPaused (A)-> onActivityResumed (B). The Count resumeCount we just mentioned is in onActivityPaused ()-In onActivityResumed () in ++, there will be A short interval based on this judgment, that is, between onPause () of A and onResume () of B, the App runs in the background, this logic is definitely incorrect.

So how to solve the problem? If you have printed the methods of the lifecycle, you will find that the steps for switching between activities are as follows:


Jump from WelcomeActivity to GestureActivity. (Here, only the onStart and onResume Callbacks are described)
A. onPause ()-> B. onStart ()-> B. onResume ()-> A. onStop ()

I estimate that 60% of students have never thought about the switch between Activities.

 

With these callbacks, we can place the count in onStart () and onStop (), so that there is no short interval. ActivityCount = 1, then it is the foreground, activityCount = 0, that is, the background. It's easy to judge.

Now let's talk about the situation and show the gesture unlocking interface.
My requirement is to display the gesture unlocking interface 10 minutes after the user locks the screen or switches to the background.
Let's split the demand. Let's talk about locking the screen and unlocking it.

This is received by BroadCastReciever. You can register it and set isScreenOff in AppStatusTracker to true every time you receive the action of lock screen ACTION_SCREEN_OFF.
Set isScreenOff to false when onActivityResumed () is called.

After switching to the background for 10 minutes, the system displays a gesture to unlock. You only need to update lastBackgroudTimestamp when onActivityStop ().

The core code is as follows:

The principle is very simple, but the knowledge points involved are very important. You can write and test your own, so don't always rely on others' code, others' class libraries. The technical implementation is very simple, however, sometimes you need to solve the problem by yourself.

 

 

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.