一、Service简介
Service的两种启动方式:context.startService() 和 context.bindService();
二、Service的启动流程
关于Service的启动流程,首先看下面的图:
context.startService的启动流程:
context.startService() ---> onCreate() ---> onStart() ---> Service running --->context.stopService() ---> onDestroy();
contetxt.bindService的启动流程:
context.bindService() --->onCreate() ---> onBind --->Service running ---> context.unbindService() ---> onDestroy();
三、 Service生命周期
Service的生命周期并不像Activity那么复杂,它只继承了onCreate()、onStart()、onDestroy()三个方法
当我们第一次启动Service时,先后调用了onCreate()、onStart()这两个方法;当停止Service时,则执行onDestroy()方法。
这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。
它可以通过Service.stopSelf()方法或者Service.stopSelfResult()方法来停止自己,只要调用一次stopService()方法便可以停止服务,无论调用了多少次的启动服务方法。
四、例子代码
MainActivity.java
package com.example.demo;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button startBtn,stopBtn,bindBtn,unbindBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn =(Button)findViewById(R.id.button1);
stopBtn = (Button)findViewById(R.id.button2);
bindBtn = (Button)findViewById(R.id.button3);
unbindBtn = (Button)findViewById(R.id.button4);
startBtn.setOnClickListener(startListener);
stopBtn.setOnClickListener(stopListener);
bindBtn.setOnClickListener(bindListener);
unbindBtn.setOnClickListener(unbindListener);
}
private OnClickListener startListener = new OnClickListener() {
//启动Service
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
};
private OnClickListener stopListener = new OnClickListener() {
//停止Service
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,MyService.class);
stopService(intent);
}
};
private OnClickListener bindListener = new OnClickListener() {
//绑定Service
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,MyService.class);
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
};
private OnClickListener unbindListener = new OnClickListener() {
//解除绑定Service
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent intent = new Intent(MainActivity.this,MyService.class);
unbindService(conn);
}
};
private ServiceConnection conn = new ServiceConnection() {
//连接对象
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_LONG).show();
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_LONG).show();
}
};
}
MyService.java
package com.example.demo;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service{
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
// bindService 时候调用该函数
Toast.makeText(this, "Play Service onBind", Toast.LENGTH_LONG).show();
mediaPlayer.start();
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "Play Service Created", Toast.LENGTH_LONG).show();
mediaPlayer = MediaPlayer.create(this, R.raw.tada);
}
@Override
public void onStart(Intent intent, int startId) {
//startService 时候调用该函数
super.onStart(intent, startId);
Toast.makeText(this, "Play Service onStart", Toast.LENGTH_LONG).show();
mediaPlayer.start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "Play Service Stopped", Toast.LENGTH_LONG).show();
mediaPlayer.stop();
}
}
完整代码:http://download.csdn.net/download/yegucheng2618/7759413
更多参考:http://blog.csdn.net/ithomer/article/details/7364024
五、startService 和 bindService 的区别
服务不能自己运行,需要通过调用COntext.startService() 或者 Context.bindService() 方法启动服务。这两种启动Service服务的应用场合有所不同。
1、使用startService() 方法启用服务,调用者与服务之间没有关系,即使调用者退出了,服务仍然在运行;使用bindService()方法启动服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止了。
2、如果打算采用Context.startService()方法启用服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onStart()方法。如果调用startService()方法前服务已经创建,多次调用startService()方法并不会导致多次创建服务,但是会多次
调用onStart()方法。采用startService()方法启动的服务,只能调用Context.stopService()方法结束服务,服务结束时会调用onDestroy()方法。
3、如果打算采用Context.bindService()方法启动服务,在服务未创建时,系统会先调用服务的onCreate()方法,接着是调用onBind()方法。这个时候调用者与服务绑定在了一起,调用者退出时,系统会先调用服务的onUnbind()方法,接着调用onDestroy()方法。如果调用bindService()方法前服务已经绑定了,多次调用bindService()方法并不会导致多次 创建服务及绑定(也就是说onCreate()与onBind()方法不会多次被调用)。如果调用者希望与正在绑定的服务解除绑定,可以调用unbindService()方法,调用该方法也会导致系统调用服务的onUnbind() ---> onDestroy() 方法。