Android Learning Note-service

Source: Internet
Author: User


* 1.Service is an application component

* 2.Service No graphical interface

* 3.Service is typically used to handle some long-consuming operations

* 4. You can use service update ContentProvider, send intent and boot system notifications, etc.

*

* 1.Service is not a separate process

* 2.Service is not a thread


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/54/07/wKiom1R1yy7ywXAyAABEHoxSIcs682.jpg "title=" Qq20141126204352.png "alt=" Wkiom1r1yy7ywxayaabehoxsics682.jpg "/>

Interface Activity_main.xml

    <button          android:id= "@+id/startbutton"         android:layout_ Width= "Fill_parent"         android:layout_height= "Wrap_content"          android:text= "Start service"/>             <Button          Android:id= "@+id/stopbutton"         android:layout_width= "Fill_parent"         android:layout_height= "Wrap_content"          android:layout_below= "@id/startbutton"          android:text= "Stop service"/> 

Firstservice.java

public class FirstService extends service{@Overridepublic ibinder onbind (Intent Intent) {System.out.println ("Service Onbind "); return null;} @Overridepublic void OnCreate () {super.oncreate (); System.out.println ("Service onCreate");} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {System.out.println ("flags-->" + flags); System.out.println ("startid-->" + Startid); System.out.println ("Service Onstartcommand"); return Super.onstartcommand (Intent, flags, Startid);} @Overridepublic void OnDestroy () {System.out.println ("Service OnDestroy"); Super.ondestroy ();}}

mainactivity.java

Public class mainactivity extends activity {/** * 1.service is an application component  *  2.service does not have a graphical interface  * 3.service is often used to handle some long-time operation  * 4. You can use the service update ContentProvider. Send intent and boot system notifications etc.  *  * 1.service is not a separate process  * 2.service not a thread  *   *  start Context.startservice ()  *  stop Context.stopservice ()  *  Activity inherits from the context so you can call the StartService () and StopService () methods directly  */private button startbutton;private  Button stopButton; @Overrideprotected  void oncreate (bundle savedinstancestate)  { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);startbutton =  (Button)  findviewbyid (R.id.startbutton);stopbutton =  (Button)  findviewbyid (R.id.stopButton); Startbutton.setonclicklistener (New startservicelistener ()); Stopbutton.setonclicklistener (new  Stopservicelistener ());} Class startservicelistener implements onclicklistener{@Overridepublic  void onclick (view v)  {Intent intent  = new intent (); Intent.setclass (Mainactivity.this, firstservice.class); StartService (Intent);}} class stopservicelistener implements onclicklistener{@Overridepublic  void onclick ( VIEW&NBSP;V)  {intent intent = new intent (); Intent.setclass (MainActivity.this,  Firstservice.class); StopService (intent);}}

Click the "Start Service" button, then click on the "Stop Service" button, the output of the console is as follows

11-26 12:42:27.697:i/system.out (1347): Service onCreate

11-26 12:42:35.057:i/system.out (1347): flags-->0

11-26 12:42:35.461:i/system.out (1347): startid-->1

11-26 12:42:35.937:i/system.out (1347): Service Onstartcommand

11-26 12:43:07.969:i/system.out (1347): Service OnDestroy



This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1582972

Android Learning Note-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.