Service is the most similar to Activity among the four Android components, and it has its own life cycle completely. However, the difference with Activity is that the Service has been running in the background and has no user interface, therefore, it is impossible to interact with users. The selection criteria of Activity and Service in a program are as follows: if a program component needs to present a certain interface to the user during running time, or the program needs to interact with the user, the Activity needs to be used, otherwise, you should consider the Service.
Create a Service:
package com.ye_yun_lin.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class TestService extends Service{@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();System.out.println("Service onCreat");}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("Service onDestroy");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("Service onStartCommand");return super.onStartCommand(intent, flags, startId);}}
Configure Service:
Start and Stop a Service:
package com.ye_yun_lin.service;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.style.ClickableSpan;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button startButton;private Button destroyButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);System.out.println("Activity onCreat");startButton=(Button)findViewById(R.id.startbutton);destroyButton=(Button)findViewById(R.id.destroybutton);startButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.setClass(MainActivity.this, TestService.class);startService(intent);}});destroyButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.setClass(MainActivity.this, TestService.class);stopService(intent);}});}}
After you click the Start Service button three times, the following message is displayed:
It can be seen that the onCreate () method will be called back each time a Service is created, and the onStart () method will be called back every time a Service is started-The onCreate method will not be called back when multiple existing Service components are started, however, the onStartCommand () method will be called back each time it is started.
The above is a simple application of the Service component. Although this Service has nothing to do, if you want this Service component to do something, then in onCreat () or onStartCommand () define the relevant business code in the method.
In the next article, we will introduce the Service in depth.