Android --- 41 --- Service Introduction, androidservice
Zookeeper
Service is the most similar component to Activity among the four Android components.
The difference between a Service and an Activity is that a Service has been running in the background and has no user interface.
The interface we see for interaction with users is an Activity, and the function that the Activity wants to implement must be a back-to-back Service that silently works.
For example, the function of sending text messages allows us to enter the recipient's mobile phone number and the content to be sent.
SmsManager is used to send text messages.
Create and configure a Service:
Defines a subclass that inherits services.
Configure the Service in the configuration file.
Method in Service:
@Overridepublic IBinder onBind(Intent intent) {return null;}
This is a required method for the Service subclass. An IBinder is returned, and the application communicates with the Service component through this object.
@Overridepublic void onCreate() {super.onCreate();}
This method will be called back immediately after the first creation.
@Overridepublic void onDestroy() {super.onDestroy();}
This method will be called back before the Service is closed.
@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {return super.onStartCommand(intent, flags, startId);}
The method is called back every time the client calls the srartService (Intent) method to start the Service.
@Overridepublic boolean onUnbind(Intent intent) {return super.onUnbind(intent);}This method is called back when all clients bound to this Service are disconnected.
Public class MainActivity extends Service {/** (non-Javadoc) ** @ see android. app. service # onBind (android. content. intent) ** required method */@ Overridepublic IBinder onBind (Intent intent) {// TODO Auto-generated method stubreturn null;}/** (non-Javadoc) ** @ see android. app. service # onCreate () *** callback when created */@ Overridepublic void onCreate () {// TODO Auto-generated method stubsuper. onCreate ();}/** (non-Javadoc) ** @ see android. app. service # callback before onDestroy () is disabled */@ Overridepublic void onDestroy () {// TODO Auto-generated method stubsuper. onDestroy ();}/** (non-Javadoc) ** @ see android. app. service # onStartCommand (android. content. intent, int, int) * callback when started */@ Overridepublic int onStartCommand (Intent intent, int flags, int startId) {// TODO Auto-generated method stubreturn super. onStartCommand (intent, flags, startId);}/** (non-Javadoc) ** @ see android. app. service # onUnbind (android. content. intent) calls back when all clients are disconnected */@ Overridepublic boolean onUnbind (Intent intent) {// TODO Auto-generated method stubreturn super. onUnbind (intent );}}
The Service configuration is almost the same as the Activity configuration.
<service android:name=".MainActivity" > <intent-filter><action android:name="com.example.servicedemo" /> </intent-filter></service>
Run Service:
There are two ways to run the Service:
There are two ways to run the Service:
1. Use the startService () method of Contex:
This method is used to start the Service. There is no association between the visitor and the Service. When the timer visitor exits, the Service is still running.
2. Use the bindService () method of Contex:
Start the Service by using this method. When a visitor is bound to the Service, the Service is terminated once the visitor exits.