Android Service Usage

Source: Internet
Author: User

AndroidIn development, when you need to createProgramService is required.

Services are different from activities (which can be understood as the difference between the background and the foreground ),

The Service startup process is as follows:

Context. startservice ()-> oncreate ()-> onstart ()-> Service Running

Here, oncreate () can initialize some services.

The process of stopping a service is as follows:

Context. stopservice () |-> ondestroy ()-> service stop

Example:

 

Public class myservice extends Service {@ overridepublic ibinder onbind (intent) {// todo auto-generated method stubreturn NULL;} @ override public void oncreate () {toast. maketext (this, "my service create", toast. length_short ). show () ;}@ overridepublic void ondestroy () {toast. maketext (this, "my service destroy", toast. length_short ). show () ;}@ overridepublic void onstart (intent, int startid) {toast. maketext (this, "my service started", toast. length_short ). show ();}}

 

Call:

 
@ Overridepublic void onclick (view v) {// todo auto-generated method stubswitch (v. GETID () {case R. id. startsevice: startservice (new intent (this, myservice. class); break; case R. id. stopservice: stopservice (new intent (this, myservice. class); break ;}}

When you call the startservice method, run the oncreate method and ondestroy method in myservice.

When the stopservice method is called, The ondestroy method is executed.

 

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.