Android's servicelearning case --here is a music recorder, which allows the servicebackend to upload the mp3file (it's burning.pdf !)

Source: Internet
Author: User

When you need to create a program running in the background, you need to use the service. Service can be divided into two types: infinite life and limited life. Note that the service and activities are different (in simple terms, it can be understood as the difference between the background and the foreground). For example, if
If you need to use service, you need to call startservice () and then use startservice () to call the oncreate () and onstart () methods in service to start a backend service.

The process of starting a service is as follows:
Below:Context. startservice ()-> Oncreate ()-> onstart ()-> Service RunningHere, oncreate () can initialize some services, while onstart () starts the service.
The process of stopping a service is as follows:Context. stopservice () |-> ondestroy ()-> service stop
The following example shows a small example of using the background service to play music. Click start to run the service and then stop the service.

Mainactivity source code:

Package com. example. test1;

Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;

Public class mainactivity extends activity {
Private button playbutton = NULL;
Private button stopbutton = NULL;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main );
Playbutton = (button) findviewbyid (R. Id. startplaybutton );
Stopbutton = (button) findviewbyid (R. Id. stopplaybutton );
Playbutton. setonclicklistener (New buttonlisten ());
Stopbutton. setonclicklistener (New buttonlisten ());
}

 

 

Class buttonlisten implements onclicklistener {

Public void onclick (view v ){
// Todo auto-generated method stub
Switch (V. GETID ()){
Case R. Id. startplaybutton:
Startservice (new intent (mainactivity. This, myservice. Class ));
Break;
Case R. Id. stopplaybutton:
Stopservice (new intent (mainactivity. This, myservice. Class ));
Break;
Default:
Break;
}
}

}
@ Override
Public Boolean oncreateoptionsmenu (menu ){
Getmenuinflater (). Inflate (R. Menu. activity_main, menu );
Return true;
}
}

Source code of myservice:

Package com. example. test1;

Import Android. App. Service;
Import Android. content. intent;
Import Android. Media. mediaplayer;
Import Android. OS. ibinder;
Import Android. widget. Toast;

Public class myservice extends Service {
Mediaplayer player = NULL;
Public myservice (){
}

@ Override
Public ibinder onbind (intent ){
// Todo: return the communication channel to the service.
Throw new unsupportedoperationexception ("not yet implemented ");
}

@ Override
Public void oncreate (){
// Todo auto-generated method stub
Super. oncreate ();
Toast. maketext (this,
"Myservice createed! ",
Toast. length_short). Show ();
Player = mediaplayer. Create (this, R. Raw. Burning );
Player. setlooping (false );
}

@ Override
Public void onstart (intent, int startid ){
// Todo auto-generated method stub
Toast. maketext (this,
"Myservice onstart! ",
Toast. length_short). Show ();
Player. Start ();
}

@ Override
Public void ondestroy (){
// Todo auto-generated method stub
Super. ondestroy ();
Player. Stop ();
Toast. maketext (this,
"Myservice ondestroy ",
Toast. length_short). Show ();
}

}

Download the source code later: http://download.csdn.net/detail/yanzi1225627/4463948

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.