Talking about one of the four major Android build service

Source: Internet
Author: User

Service is one of the four main components of Android, the individual feel that service usage or relatively simple amount, can be understood as invisible activity, because service usage and activity is very close. Start-up, life cycle, etc., are very simple.

Below we will learn the service through simple code.

The first is the service file, Myservice.java, very simple, and activity, inheriting a class, overriding the relevant method is good, at the same time, it should be noted that the four components are to be registered in the manifest file

<android:name= ". MyService "/>

 PackageCom.example.servicedemo;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.IBinder;/***************************************************************** * @ Copyright Linfeng * * @ author Linfeng * * @ VERSION 1.0 * * @ Created Date 2016-6-8 * * @ function Description ******************************************************************/ Public classMyServiceextendsservice{/*** The only abstract method in the service that must be implemented in the subclass*/@Override Publicibinder onbind (Intent Intent) {return NULL; }        /*** Called when the service is created*/@Override Public voidonCreate () {Super. OnCreate (); }        /*** Every time the service is started, this case, the start service will execute every click * OnCreate is only the first time to click on the execution*/@Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {        return Super. Onstartcommand (Intent, flags, Startid); }        /*** Call when service is destroyed*/@Override Public voidOnDestroy () {Super. OnDestroy (); }}

And then the mainactivity file and his layout file.

 PackageCom.example.servicedemo;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidonclick (View v) {Switch(V.getid ()) { CaseR.id.bt_start:/*** Services and activities, like the use of intent to start, in fact, the service can be understood as no interface activities*/Intent startintent=NewIntent ( This, MyService.class);                StartService (startintent);  Break;  Caser.id.bt_stop:intent stopintent=NewIntent ( This, MyService.class);                StopService (stopintent);  Break; default :                 Break; }    }}
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.servicedemo.MainActivity" >        <ButtonAndroid:onclick= "onclick"Android:id= "@+id/bt_start"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Open service"        />         <ButtonAndroid:onclick= "onclick"Android:id= "@+id/bt_stop"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Close Service"        /></LinearLayout>

Then it is run, notice, go back to look for settings, to press the home button, instead of the return button OH.

Talking about one of the four major Android build service

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.