启动状态(Bind):
当应用组件(如 Activity)通过调用 startService() 启动服务时,服务即处于“启动”状态。一旦启动,服务即可在后台无限期运行,即使启动服务的组件已被销毁也不受影响,除非手动调用才能停止服务,已启动的服务通常是执行单一操作,而且不会将结果返回给调用方。
绑定状态(Start):
当应用组件通过调用 bindService() 绑定到服务时,服务即处于“绑定”状态。绑定服务提供了一个客户端-服务器接口,允许组件与服务进行交互、发送请求、获取结果,甚至是利用进程间通信 (IPC)跨进程执行这些操作。 仅当与另一个应用组件绑定时,绑定服务才会运行。 多个组件可以同时绑定到该服务,但全部取消绑定后,该服务即会被销毁。
onCreate()
首次创建服务时,系统将调用此方法来执行一次性设置程序(在调用 onStartCommand() 或onBind() 之前)。如果服务已在运行,则不会调用此方法,该方法只调用一次。
onStartCommand()
当另一个组件(如 Activity)通过调用 startService() 请求启动服务时,系统将调用此方法。一旦执行此方法,服务即会启动并可在后台无限期运行。 如果自己实现此方法,则需要在服务工作完成后,通过调用 stopSelf() 或 stopService() 来停止服务。(在绑定状态下,无需实现此方法。)
onBind()
当另一个组件想通过调用bindService()与服务绑定(例如执行 RPC)时,系统将调用此方法。在此方法的实现中,必须返回 一个IBinder接口的实现类,供客户端用来与服务进行通信。无论是启动状态还是绑定状态,此方法必须重写,但在启动状态的情况下直接返回 null。
onDestroy()
当服务不再使用且将被销毁时,系统将调用此方法。服务应该实现此方法来清理所有资源,如线程、注册的侦听器、接收器等,这是服务接收的最后一个调用。
代码案例:<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="启动服务"
android:onClick="startMyService"
/>
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="60dp"
android:text="绑定服务"
android:onClick="bindMyService"
/>
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="120dp"
android:text="停止服务"
android:onClick="stopMyService"
/>
package com.what21.myservice;import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startMyService(View view){
Intent intent = new Intent(this,MyService.class);
intent.putExtra("name","value");
Log.d(TAG,"startMyService()");
super.startService(intent);
}
public void bindMyService(View view){
Intent service = new Intent(this, MyService.class);
MyConnection myConection = new MyConnection();
//第一个参数:Intent意图
//第二个参数:是一个接口,通过这个接口接收服务开启或者停止的消息,并且这个参数不能为null
//第三个参数:开启服务时的操作,BIND_AUTO_CREATE代表自动创建service
super.bindService(service, myConection, BIND_AUTO_CREATE);
}
public void stopMyService(View view){
Intent intent = new Intent(this,MyService.class);
intent.putExtra("name","value");
Log.d(TAG,"stopMyService()");
super.stopService(intent);
}
}package com.what21.myservice;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
public class MyConnection implements ServiceConnection {
private static final String TAG = "MyConnection";
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//只有当我们自己写的MyService的onBind方法返回值不为null时,才会被调用
Log.d(TAG,"onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
//这个方法只有出现异常时才会调用,服务器正常退出不会调用。
Log.e(TAG,"onServiceDisconnected");
}
}package com.what21.myservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int result = super.onStartCommand(intent, flags, startId);
Log.d(TAG,"onStartCommand()" + result);
return result;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
MyBinder myBinder = new MyBinder();
Log.d(TAG,"onBind()" + myBinder.toString());
return myBinder;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy()");
}
private class MyBinder extends Binder {
public void systemOut(){
Log.d(TAG,"该方法在MyService的内部类MyBinder中");
}
}
}
日志输出点击:启动服务,停止服务
05-04 05:23:04.839 8430-8430/com.what21.myservice D/MainActivity: startMyService()
05-04 05:23:04.874 8430-8430/com.what21.myservice D/MyService: onCreate()
05-04 05:23:04.875 8430-8430/com.what21.myservice D/MyService: onStartCommand()1
05-04 05:23:18.308 8430-8430/com.what21.myservice D/MainActivity: stopMyService()
05-04 05:23:18.333 8430-8430/com.what21.myservice D/MyService: onDestroy()
点击:绑定服务,停止服务
05-04 05:25:00.112 8488-8488/com.what21.myservice D/MyService: onCreate()
onBind()com.what21.myservice.MyService$MyBinder@981c8e2
05-04 05:25:00.155 8488-8488/com.what21.myservice D/MyConnection: onServiceConnected
05-04 05:25:11.697 8488-8488/com.what21.myservice D/MainActivity: stopMyService()