It’s unlikely that the system will kill a foreground Service.
Typically we’d use foreground Services for work that the user is aware of, like playing music.
When we use a foreground Service, we have to send a notification to:
Our tutorial app shows you how to use Services to:
There are two parts to the tutorial: Part 1 shows you how to use a simple Service and Part 2 shows you how to use a foreground Service.
Services are app components that you can use to do work in the background. They don’t have a User Interface.
They are started and stopped by other components (like Activities, Broadcast Receivers and other Services).
You can also share Services with other apps.
Have you read the article on Processes and Threads yet?
Always remember the two most important rules when working with threads:
Speed up your apps response times. Move all processing and I/O operations off the main thread. Do the work in a child thread. Our tutorial will show you how.
All Android apps run in their own process and by default, in a single thread.
Android follows a multitasking design. This allows a number of applications to run at the same time. The problem is that all these apps need memory which is in short supply.
We only have one activity in our app. It hosts the fragments.
If you haven't already done so, have a look at :
We’re using the support library so make sure that you import the correct class:
Our tutorial app has one activity and two fragments. One fragment contains a list and the other an image.
Running the app on a small device in portrait mode, displays the list fragment. Selecting an item in the list displays the second fragment containing an image.
Flipping the device to landscape displays both fragments side by side.
Running the app on a tablet displays the fragments side by side in both portrait and landscape mode.
Think of components like buttons and images. You can reuse them over and over in different activities. You can also rearrange them within an activity.
Fragments are similar.
Fragments let you divide your activity into reusable components which have their own User Interface and lifecycle.
The data that is saved by the system to restore the previous state of an activity is called the Instance State. This data is saved as a collection of key/value pairs in a Bundle object.