简体   繁体   中英

What does it happen when I locked the screen of my device in Android

I am working on an Android application and it works as I want. Nevertheless, some of the processes stop being executed when I lock the screen of my device. Since I do not really understand what is the behaviour of Android when this situation is triggered, I would like to get a clarification on this topic.

For example, when I switch the orientation of my device a new lifecycle is conducted on the application. This would be the behaviour of Android on this situation. What I do not understand is how is different to run an application in the foreground or background, and if possible, to get also information about what is the common solution to run a process in Android, or even further, and example.

Recently there has been a lot of focus in the Android OS development on extending battery life. One of the things that has become severely restricted is what an app can do when it's backgrounded.

Whenever your app is not the foreground app, its behaviours are severely restricted. Essentially your app does nothing when it's backgrounded unless you've done some of the specific things that Android requires you to do in order to run in the background.

This applies whether your app was put into the background because you're using another app, or because the screen is locked.

Here are some links that talk about background execution. From memory Oreo was the version where it started getting tricky in my app.

https://developer.android.com/about/versions/oreo/background

This is probably the best reference for what you need to do now: https://developer.android.com/guide/background

What happens immediately when you lock the screen is the standard activity / fragment lifecycle onPause(),a nd possibly onStop()

https://developer.android.com/guide/components/activities/activity-lifecycle#:~:text=to%20App%20Architecture.-,Activity-lifecycle%20concepts,activity%20enters%20a%20new%20state .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM