The last time we introduced the use of Activity and Intent, this time we will introduce Service. If Activity is compared to a foreground program, Service is a background program, and the entire life cycle of Service is only executed in the background. Service is also called by Intent like Activity. To add a Service in the project, first create a class that inherits the Service, and then add it to the Service tag in AndroidManifest. xml-> Application Nodes.
The Service is started by the Activity through startService or bindService, and the Intent is responsible for passing parameters. First paste the program running this article:
This article mainly describes the Service call and its lifecycle.
Is the Service status change of stopService after startService.
Is the Service status change of unbindService after bindService.
StartService and bindService can both start the Service. What is the difference between them? The difference between them is to change the Service cycle. The Service started by startService must have stopService to end the Service. If you do not call stopService, the Activity ends and the Service is still running. The Service started by bindService can be ended by unbindService, or automatically ends after the Activity ends (onDestroy.
The Service status of Activity. finish () changes after startService, and the Service is still running.
Is the Service status change of Activity. finish () after bindService, the Service automatically unbindService.
Main. xml code:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnStartMyService"
Android: text = "StartMyService"> </Button>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnStopMyService"
Android: text = "StopMyService"> </Button>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnBindMyService"
Android: text = "BindMyService"> </Button>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnUnbindMyService"
Android: text = "UnbindMyService"> </Button>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnExit"
Android: text = "Exit program"> </Button>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnStartMyService"
Android: text = "StartMyService"> </Button>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnStopMyService"
Android: text = "StopMyService"> </Button>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnBindMyService"
Android: text = "BindMyService"> </Button>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnUnbindMyService"
Android: text = "UnbindMyService"> </Button>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/btnExit"
Android: text = "Exit program"> </Button>
</LinearLayout>
Source code of testService. java:
View plaincopy to clipboardprint?
Package com. testService;
Import android. app. Activity;
Import android. app. Service;
Import android. content. ComponentName;
Import android. content. Intent;
Import android. content. ServiceConnection;
Import android. OS. Bundle;
Import android. OS. IBinder;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Public class testService extends Activity {
Button btnStartMyService, btnStopMyService, btnBindMyService, btnUnbindMyService, btnExit;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
BtnStartMyService = (Button) this. findViewById (R. id. btnStartMyService );
BtnStartMyService. setOnClickListener (new ClickEvent ());
BtnStopMyService = (Button) this. findViewById (R. id. btnStopMyService );
BtnStopMyService. setOnClickListener (new ClickEvent ());
BtnBindMyService = (Button) this. findViewById (R. id. btnBindMyService );
BtnBindMyService. setOnClickListener (new ClickEvent ());
BtnUnbindMyService = (Button) this. findViewById (R. id. btnUnbindMyService );
BtnUnbindMyService. setOnClickListener (new ClickEvent ());
& Nbs