Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the life cycle of Service in Android

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly talks about "what is the life cycle of Service in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the life cycle of Service in Android"?

The Android Service lifecycle can promote the innovation of mobile devices and let users experience the more mobile services. It is activated only when broadcast receivers executes this method, and when onReceive () returns, it is inactive.

If there is no program to stop it or if it stops itself, service will keep running. In this mode, service starts with a call to Context.startService () and stops at Context.stopService (). Service can stop itself by calling the Android Service life cycle () or Service.stopSelfResult (). No matter how many times you call startService (), you only need to call stopService () once to stop service.

It can be called by an external program through the interface. The external program establishes a connection to the service and operates the service through the connection. The connection establishment call starts with Context.bindService () and ends with Context.unbindService (). Multiple clients can bind to the same service, and if service is not started, bindService () can choose to start it.

The two modes are not completely separate. You can bind to a service started by startService (). If an intent wants to play music, start the service that plays music in the background through the startService () method. Then, if the user wants to manipulate the player or get information about the music currently playing, an activity will establish a connection to the service through bindService (). In this case, stopService () will not actually stop service until all connections are closed.

Like activity, service has a lifecycle that can be achieved by monitoring status. But less than activity-- only three-- and it's public's, not protected's.

Void onCreate () void onStart (Intent intent) void onDestroy ()

By implementing these three methods, you can monitor the two nested loops of the service lifecycle:

The entire life cycle starts with onCreate () and ends with onDestroy (). Like activity, the an Android Service life cycle performs initialization operations in onCreate () and frees all used resources in onDestroy (). For example, a service that plays music in the background may create a thread to play music in onCreate () and destroy the thread in onDestroy ().

The activity life cycle begins with onStart (). This method handles the intent passed into the startService () method. The music service opens intent to see which song to play and starts playing. When the service stops, there is no method to detect-- there is no onStop () method, onCreate () and onDestroy () are used for all service started with Context.startService () or Context.bindService (). OnStart () is only used for service that starts with startService ().

If an Android Service lifecycle can be externally bound, it can trigger the following methods:

IBinder onBind (Intent intent) boolean onUnbind (Intent intent) void onRebind (Intent intent)

The onBind () callback is passed to the intent that calls bindService, and onUnbind () is handled by intent in unbindService (). If the service allows binding. Then the onBind () method returns the communication channel between the client and the sercie. If a new client connects to the service, onUnbind () triggers the onRebind () call.

The following chart illustrates the callback method of sercice. The following images separate the startService from the service launched by bindService (), but note that no matter how they are started, they may be connected by the client, so they may trigger the onBind () and onUnbind () methods.

When broadcast message arrives after a receiver request, Android calls the onReceive () method of the intent that holds message. It is activated only when broadcast receivers executes this method, and when onReceive () returns, it is inactive.

A process that contains an active broadcast receiver will not be aborted. But a process that contains only inactive components can be aborted at any time when the memory it occupies is requested by other programs. A problem occurs when a program that responds to broadcast message consumes a lot of time and processes on a thread other than the thread on which the UI resides.

When onReceive () starts a thread and returns, the entire program (including the newly created thread) state is inactive (unless there are other activated components in the process), so the process is in danger of being aborted. The solution to this problem is for the onReceive () method to start an Android Service life cycle and let sercie do the time-consuming work so that the system knows that there is still active work in the process.

At this point, I believe you have a deeper understanding of "what is the life cycle of Service in Android?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report