Android Service Lifecycle and usage

Source: Internet
Author: User

I explained the lifecycle of the Android activity in the previous section, and I'll explain the service, and first we need to know what the service is for. And the life cycle of it.

Service Concept and use:

Android services, it is different from the activity, it can not interact with the user, can not start their own, running in the background of the program, if we quit the application, the service process is not over, it is still running in the background, then when we will use the service? Like when we play the music, might want to listen to music while doing some other things, when we quit playing the music application, if not service, we can not hear the song, so this time we have to use service, and for example, when we have an application of data is obtained through the network, The data at different times (for a while) is different at this time we can use the service in the background to update the time, and not every open the application when to get.

Service life cycle:

The life cycle of the Android service is not as complex as the activity, it inherits only the OnCreate (), OnStart (), OnDestroy () three methods, and when we first started the service, we called OnCreate (), OnStart () These two methods, when the service is stopped, the OnDestroy () method is executed, and it should be noted that if the service is started, the OnCreate () method is not executed when we start the service again. Instead, it directly executes the OnStart () method, which can be seen in the example below.

Service and Activity communication:

The data at the back end of the service is ultimately to be present on the front-end activity, because the system will reopen a new process when the service is started, which involves communication between different processes (AIDL) I do not describe this section too much, When we want to get the started service instance, we can use the Bindservice and Onbindservice methods, which perform the IBinder () and Onunbind () methods of the service respectively.

In order to make it easier for everyone to understand, I wrote a simple demo, you can imitate me, step-by-step.

Step one: Create a new Android project, which I'm naming Servicedemo.

Step Two: Modify the Main.xml code, I have added four buttons here, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" 
    Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " > <textview android:id= "@+id/text" android:layout_width= fill_parent "android:layout_height=" WR Ap_content "android:text=" @string/hello "/> <button android:id=" @+id/startservice "Android:layout_wi Dth= "Fill_parent" android:layout_height= "wrap_content" android:text= "StartService"/> <Button android:id= "@+ Id/stopservice "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:text=" StopService "/> <button android:id=" @+id/bindservice "android:layout_width=" fill_parent "android:layout_height=" Wrap_co Ntent "android:text=" Bindservice "/> <button android:id=" @+id/unbindservice "android:layout_width=" Fill_pare NT "android:layout_height=" Wrap_coNtent "android:text=" Unbindservice "/> </LinearLayout> 

Related Article

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.