Android Service components (1)

Source: Internet
Author: User

Android Service components (1)
Like other services, android service does not have an actual running interface. It runs on the android background. Generally, services are used to provide services for applications (such as downloading files from the Internet and controlling music players ). The lifecycle of a Service is much simpler than that of an activity. It has only three stages (creating a Service, starting a Service, and destroying a Service ). The following describes the service components of android through examples. 1. Create the android project. Create the android project android_service_one in Eclipse. 2. Create the Service. In the android project, create the package com. example. service and add the method MyService. This method inherits from the Service. This class is used to display the three lifecycles of a service. Copy the code package com. example. service; import android. app. service; import android. content. intent; import android. OS. IBinder; import android. util. log;/***/public class MyService extends Service {@ Override public IBinder onBind (Intent intent) {// TODO Auto-generated method stub return null ;} // call public void onCreate () {Log when you open it for the first time. d ("MyService", "onCreate"); super. onCreate ();} // call public void onDe when stopping Story () {Log. d ("MySerVice", "onDestory"); super. onDestroy () ;}// call public void onStart (Intent intent, int startId) {Log. d ("MyService", "onStart"); super. onStart (intent, startId) ;}} copy code 3. the called service calls the service just created in the android Activity component. before calling the service, you need to call it in AndroidManifest. configure the service in xml. Use <service> </service> to configure service components. 1 <service android: enabled = "true" android: name = "com. example. service. MyService"> </service> after the service configuration is complete, you can call this service. The interaction between the service and the activity still communicates through Intent. Startservice (intent) is used to start the service, and stopservice (Intent intent) is used to stop the service. Intent declaration: Intent = new Intent (this, MyService. class); copy code 1 public void onClick (View v) {2 // TODO Auto-generated method stub 3 switch (v. getId () 4 {5 case R. id. button1: 6 startService (serviceintent); 7 break; 8 case R. id. button2: 9 stopService (serviceintent); 10 break; 11} 12} copy code 4. the Service and Activity are bound to the Service and Activity. When the Activity is started, the Service starts automatically. When the Activity is destroyed, the service is also destroyed. Here, we need to rewrite several methods in MyService, such as onBind, onRebind, and onUnbind. Copy code 1 package com. example. service; 2 3 import android. app. service; 4 import android. content. intent; 5 import android. OS. binder; 6 import android. OS. IBinder; 7 import android. util. log; 8 9/* 10*11 */12 public class MyService extends Service {13 14 private MyBinder myBinder = new MyBinder (); 15 16 @ Override17 // call this method when binding successfully 18 public IBinder onBind (Intent intent) {19 // TODO Auto-generated method stub 20 Log. d ("MyService", "onBind"); 21 return myBinder; 22} 23 24 // call this method when rebinding 25 public void onRebind (Intent intent) 26 {27 Log. d ("MyService", "onRebind"); 28 super. onRebind (intent); 29} 30 31 // call this method when unbinding 32 public void onUnBind (Intent intent) 33 {34 Log. d ("MyService", "onUnbind"); 35 super. onUnbind (intent); 36} 37 // call 38 public void onCreate () 39 {40 Log at the first open. d ("MyService", "onCreate"); 41 super. onCreate (); 4 2} 43 // call 44 public void onDestory () 45 {46 Log. d ("MySerVice", "onDestory"); 47 super. onDestroy (); 48} 49 // call 50 public void onStart (Intent intent, int startId) 51 {52 Log. d ("MyService", "onStart"); 53 super. onStart (intent, startId); 54} 55 56 // bind class 57 public class MyBinder extends Binder58 {59 // return service instance 60 public MyService getService () 61 {62 return MyService. this; 63} 64} 65} copy Code 5. call the service to declare the service in the Activity Variable Myservice myService, and the connection variable for declaring ServiceConnection. BindService (serviceintent, conn, Context. BIND_AUTO_CREATE); call to unbind, unbindService (ServiceConnection ). copy code 1 Intent serviceintent; 2 private MyService myService; 3 private ServiceConnection conn = new ServiceConnection () {4 5 @ Override 6 public void onServiceDisconnected (ComponentName name) {7 // TODO Auto-generated method stub 8 myService = null; 9 Toast. makeText (MainActivity. this, "service connection failed", Toast. LENGTH_LONG ). show (); 10} 11 12 @ Override13 public void onServiceConnected (ComponentName, IBinder service) {14 // TODO Auto-generated method stub15 myService = (MyService. myBinder) service ). getService (); 16 Toast. makeText (MainActivity. this, "service connection successful", Toast. LENGTH_LONG ). show (); 17} 18}; copy the code

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.