Android improves Service in Article 5

Source: Internet
Author: User

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

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.