Determine if the app is running in the background

Source: Internet
Author: User

In some scenarios, it is often necessary to determine whether the app is running in the background, such as whether the unlock interface is displayed, whether new messages are displayed notification, etc. The demand may be diversified, but the principle is interlinked, today stay intends to talk about the optimal solution of these requirements.

Of course, stay certainly won't say go to the for loop to judge the current runningprocess or runningtasks. Like what:

Such

Or so

It feels like you're using a task manager in a Windows system, and it really hurts. We do not care about performance issues, it is said that why Android even a decent API does not give me, really frustrating.

If you take this kind of questioning to investigate, you will find that there are other ways to achieve it.

Android provides a callback when it comes to SDK 14. activitylifecyclecallbacks, you can get the lifecycle callback for all of the app's activity through this callback. Look at the picture:

This callback is written in application, and you can register it when the application is initialized. We can write a singleton class to cache these status. Here I call it appstatustracker. In application's OnCreate () let Appstatustracker register Activitylifecyclecallbacks.

What's the use of getting these callback, and how can I know if the app is running in the foreground?


Don't worry, let's talk about the life cycle of the activity first. This is an interview problem, although there are official answers, but the real understanding of the life cycle, and flexible use of a few.

Let's imagine if the activity calls Onresume (), then the activity must be visible, that is, running in the foreground. If OnPause () is called, and there is no activity to invoke Onresume (), then the app runs backstage. I don't care if it's the home button or the Back button.

With this judgment, we use the Activitylifecyclecallbacks callback's onactivityresumed () and onactivitypaused () methods to count, if there is only one activitycount, Then the current app in the foreground, if the wood has activitycount, it is in the background.

OK, so happy to solve, no longer for loop. But soon you will find that there is a delay, which can lead to inaccurate judgment.

Let's say we have two activities, a, a, B, jump from A to B, how does the life cycle go? A.onpause (), B.onresume () corresponds to Activitylifecyclecallbacks onactivitypaused (A), onactivityresumed (B), The counting resumecount that we said just now, in onactivitypaused ()-, in onactivityresumed () + +, according to this judgment there will be a short interval, that is, a onpause () to B onresume ( ), the app is running in the background, so the logic is definitely wrong.

How to solve the problem? If you print out which methods of the life cycle, you will find that the steps to switch between the activity are as follows:


Jump from Welcomeactivity to gestureactivity. (Just say OnStart, onresume these callbacks)
A.onpause (), B.onstart (), B.onresume (), A.onstop ()

I reckoned 60% of my classmates did not think of the activities between the life cycle of the transition.

With these callbacks we can put this count in OnStart () and OnStop () so that there is no such a short interval. Activitycount==1, then is the front desk, activitycount==0, that is backstage. This is a very simple judgment.

Now again, what happens when the gesture unlocking interface is displayed.
My requirement is to display the gesture unlock screen after the user has locked the screen and then unlocked or switched to the background for 10 minutes.
We split the demand, first said the lock screen, unlocked.

This is a broadcastreciever to receive, registration can be, each time you receive the lock screen Action_screen_off action, the Appstatustracker in the Isscreenoff set to True.
When Onactivityresumed () is called, the Isscreenoff is set to false.

Again, after switching to the background for 10 minutes, the display gesture is unlocked. This only needs to be updated under Onactivitystop () Lastbackgroudtimestamp.

The core code is as follows:

The principle is very simple, but the knowledge point involved is very important, we can write their own test, do not always rely on others code, others of the class library, technology implementation is very simple, but the needs of the variant extension sometimes need to find a way to solve.

 

Determine if the app is running in the background

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.