Overview of basic usage of startservice in Android

There are two main ways to use service in Android. By calling the startservice method of context or the bindservice method of context, this paper only discusses the use of pure startservice and does not involve any bindservice method call. If you want to know about the use of bindservice, please refer to overview of basic usage of bindservice in Android.

When we call the startservice method of context, we start the service. The service started through the startservice method will run indefinitely. The service will stop running and be destroyed only when we call the stopservice of context externally or the stopself method of service internally.

To use a service, we first inherit from the service, and then rewrite the following methods: oncreate, onstartcommand, onbind, and ondestroy.

These methods are callback methods, which are called by the Android operating system at the right time, and it should be noted that these callback methods are called in the main thread.

Oncreate: when executing the startservice method, if the service is not running, it will create the service and execute the oncreate callback method of the service; If the service is already running, executing the startservice method will not execute the oncreate method of the service. That is, if the startservice method of context is executed multiple times to start the service, the oncreate method of the service method will only be called once when the service is created for the first time, and will not be called again in the future. We can complete some operations related to service initialization in oncreate method.

Onstartcommand: after the startservice method is executed, the oncreate method of the service may be called. After that, the onstartcommand callback method of the service must be executed. That is, if the startservice method of context is executed multiple times, the onstartcommand method of service will be called multiple times accordingly. Onstartcommand method is very important. In this method, we carry out actual operations according to the passed intent parameter. For example, a thread will be created here to download data or play music.

Onbind: the onbind method in the service is an abstract method, so the service class itself is an abstract class, that is, the onbind method must be overridden, even if we can't use it. When using the service through startservice, we only need to return null when overriding the onbind method. The onbind method is mainly used when calling service for the bindservice method.

Ondestroy: the service started through the startservice method will run indefinitely. The service will stop running and be destroyed only when the stopservice of context is called or the stopself method is called inside the service. The service callback function will be executed when it is destroyed.

In order to explore the service life cycle started by the startservice method to verify the above description of each callback function method, we wrote the following test case. First, create a service class testservice, which inherits from service. The code is as follows:

We simply printed out the corresponding information in each callback method of testservice, and did not do many complex processing operations.

Then we call the Serivce in Activity, and the corresponding code in Activity is as follows:

In Activity, we first called Activity's startService method for three consecutive times to start Service, then called stopService method of Activity to stop Service, and then invoked Activity's startService method to start Service.

The output results of the running program are as follows:

We analyze the above output results. First, we print out that the ID of the main thread is 1, and then we find that the ID of all execution threads printed in the callback function is 1, which shows that each callback method in the service runs in the main thread. Secondly, we can find that after calling the startservice method three times in a row, we only trigger the oncreate callback method once and the onstartcommand method three times. In onstartcommand, we can read the intent object passed in through the startservice method, and the startids of the three times are different, namely 1, 2 and 3, Every time you call startservice, a startid will be automatically assigned. Startid can be used to distinguish the calls of different startservices. Generally, startid is counted from 1. After each call to startservice, startid will be automatically incremented by one.

Then we called the stopservice (intent4) method of activity to stop the service. Through the output results, we found that the service executed the ondestroy method. Generally, we can perform some resource release operations in the ondestroy method. After ondestroy is executed, the instance of the service is destroyed. Although we have called the three startService method before, once we call stopService once, we can stop the running Service and destroy it.

Finally, when we start the service again through startservice (intent5), through the output results, we find that the oncreate method of the service is executed again, which indicates that the service is re created after being destroyed through stopservice, and then the onstartcommand callback method is called again, and the startid starts counting from 1 again.

Finally, it should be noted that we wrote two output codes at the beginning and end of the operation service in the activity, which are

and

However, when we look at the output results, we will find that the program directly outputs after test startservice after it outputs before test startservice, and then the output of each callback method in testservice. This shows that the startservice () method and stopservice () method return immediately after execution, that is, neither of these two methods is blocking, Starting and stopping services are asynchronous operations. Startservice() and stopservice() send intent objects to the Android framework, and then the framework layer starts and stops services asynchronously.

Let's use a diagram to summarize the life cycle of a service started through startservice:

When Android is faced with a lack of memory, it may destroy your currently running service, and then re create the service when the memory is sufficient. The behavior that the service is forcibly destroyed and re rebuilt by the Android system depends on the return value of onstartcommand method in the service. There are three common return values, start_ NOT_ STICKY、START_ Stick and start_ REDELIVER_ Intent, these three values are static constants in service.

START_ NOT_ Sticky: if return to start_ NOT_ Sticky means that when the process running the service is forcibly killed by the Android system, the service will not be re created. Of course, if startservice is called for a period of time after it is killed, the service will be instantiated. In what situation is it appropriate to return this value? If the work performed by a service is interrupted several times, which does not matter, or it is acceptable that it needs to be killed when Android memory is tight and will not be recreated immediately, then we can set the return value of onstartcommand to start_ NOT_ STICKY。 For example, a service needs to regularly obtain the latest data from the server: use a timer to start the service every specified n minutes to obtain the latest data on the server. When the onstartcommand of the service is executed, a timer after n minutes is planned in the method to start the service again and open up a new thread to perform network operations. Suppose that the service is forcibly killed by the Android system during the process of obtaining the latest data from the server, and the service will not be re created, which doesn't matter, because the timer will start the service again and obtain the data again in n minutes.

START_ Sticky: if return to start_ Sticky means that after the running process of the service is forcibly killed by the Android system, the Android system will still set the service to the started state (i.e. running state), but will no longer save the intent object passed in by the onstartcommand method. Then the Android system will try to re create the service again and execute the onstartcommand callback method, However, the intent parameter of the onstartcommand callback method is null, that is, although the onstartcommand method will execute, it cannot obtain the intent information. If your service can run or end at any time without any problem, and does not need intent information, you can return start in the onstartcommand method_ Sticky, such as a service used to play background music, is suitable to return this value.

START_ REDELIVER_ Intent: if it returns start_ REDELIVER_ Intent, which means that after the process running the service is forcibly killed by the Android system, it returns to start_ The situation of sticky is similar. The Android system will recreate the service again and execute the onstartcommand callback method. However, the difference is that the Android system will retain the intent last passed into the onstartcommand method before the service is killed again and pass it into the onstartcommand method of the recreated service again, So we can read the intent parameter. Just return to start_ REDELIVER_ Intent, then onstartcommand's heavy intent must not be null. If our service needs to rely on a specific intent to run (it needs to read relevant data information from the intent), and it is necessary to re create the operation after forced destruction, such a service is suitable to return to start_ REDELIVER_ INTENT。

The above is an overview of the basic use of startservice and its life cycle. I hope it will be helpful to your study

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>