Service of Android four components

Source: Internet
Author: User

A service is a component that runs in the background without an interface, starting with another component invocation. The services in Android and services in Windows are similar, it runs in the system is not easy to be detected by the user, you can use it to develop such as monitoring programs.

Implementation of services (service)

1. Define a class EchoService and inherit from service, rewrite OnCreate (), OnStart (Intent Intent, int startid), onbind (Intent Intent), Onunbind ( Intent Intent), OnDestroy () the required method.

1  PackageHx.cn.usingservice;2 3 ImportAndroid.app.Service;4 Importandroid.content.Intent;5 ImportAndroid.os.IBinder;6 7 /**8 * Created by YQ on 2015/11/3.9  */Ten  Public classEchoServiceextendsService { One @Override A      Publicibinder onbind (Intent Intent) { -         return NULL; -     } the  - @Override -      Public voidonCreate () { -         Super. OnCreate (); +System.out.println ("Oncreat ()"); -     } +  A @Override at      Public voidOnDestroy () { -         Super. OnDestroy (); -System.out.println ("Ondestory ()"); -     } -}

2. Declare <service> under the <application> node of the manifest file.

1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3      Package= "Hx.cn.usingservice" >4 5<Application6Android:allowbackup= "true"7android:icon= "@mipmap/ic_launcher"8Android:label= "@string/app_name"9Android:theme= "@style/apptheme" >Ten<Activity OneAndroid:name= ". Mainactivity " AAndroid:label= "@string/app_name" > -<intent-filter> -<action android:name= "Android.intent.action.MAIN"/> the  -<category android:name= "Android.intent.category.LAUNCHER"/> -</intent-filter> -</activity> +         <service android:name= ". EchoService "></service> -</application> +  A</manifest>

Note: The service cannot run on its own and needs to start the service by calling the Context.startservice () or Context.bindservice () method. Both of these methods can start the service, but they are used in different situations. Using the StartService () method to enable the service, there is no association between the visitor and the service, and the service is still running even if the visitor exits. Using the Bindservice () method to enable the service, the visitor is bound together with the service, and once the visitor exits, the service terminates. Using the Context.startservice () method to start the service, only the Context.stopservice () method can be called to end the service, and the OnDestroy () method is called at the end of the service.

3. The following are the main codes implemented in Mainactivity:

1 PrivateButton Btn_startservice,btn_stopservice;2     PrivateIntent Intentservice;3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8Intentservice=NewIntent ( This, EchoService.class);9 TenBtn_startservice=(Button) Findviewbyid (r.id.btn_startservice); OneBtn_stopservice=(Button) Findviewbyid (r.id.btn_stopservice); A  -Btn_startservice.setonclicklistener ( This); -Btn_stopservice.setonclicklistener ( This); the     } -  -  -  +  -  +  A  at @Override -      Public voidOnClick (view view) { -         Switch(View.getid ()) { -              CaseR.id.btn_startservice: - StartService (intentservice); -                  Break; in              CaseR.id.btn_stopservice: - StopService (intentservice); to                  Break; +}

Service of Android four components

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.