先看一段代码在LogCat中添加一个标签,观察Service生命周期的过程。
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class ServiceLeft extends Service {
private static final String TAG = "ServiceLeft";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
// //进行创建
Log.i(TAG, "onCreate");
super.onCreate();
}
@Override
public void onDestroy() {
// 进行销毁
Log.i(TAG, "onDestroy");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// 进行开始
Log.i(TAG, "onStart");
super.onStart(intent, startId);
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements OnClickListener {
private Button llbstatr;
private Button llbstop;
private Intent serviceIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
llbstatr = (Button) findViewById(R.id.llbstart);
llbstop = (Button) findViewById(R.id.llbstop);
llbstatr.setOnClickListener(this);// 绑定器
llbstop.setOnClickListener(this);
serviceIntent = new Intent(this, ServiceLeft.class);
// 跳转ServiceLeft生命周期并且调用方法
}
@Override
public void onClick(View v) {
// 根据按钮,触发事件
switch (v.getId()) {
case R.id.llbstart:
startService(serviceIntent);
break;
case R.id.llbstop:
stopService(serviceIntent);
break;
}
}
}
一个从service播放音乐的音乐播放器,应被设置为前台运行,因为用户会明确地注意它的运行.在状态栏中的通知可能会显示当前的歌曲并且允许用户启动一个activity来与音乐播放器交互。
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message),pendingIntent);
startForeground(ONGOING_NOTIFICATION,notification);
要请求你的service运行于前台,调用startForeground().此方法有两个参数:一个整数唯一的标识一个通知,和这个用于状态栏的通知,例如:要从前台移除service,调用stopForeground()。这个方法有boolean型参数,表明是否也从状态栏删除对应的通知.这个方法不会停掉service。然而,如果你停止了正在前台运行的service,这个通知也会被删除。
注意:方法startForeground()和方法stopForeground()是从Android2.0 (API Level 5)引入的。为了在早期版本是于前台运行你的service,你必须使用以前的那个setForeground()方法—见startForeground()的API文档查看如何提供与旧版本的兼容性。
一个service的生命期比一个activity要简单得多。然而,你依然需要密切关注你的service是如何被创建又是如何被销毁的,因为一个service可以运行于后台而用户看不到它。
ervice的生命期—从它被创建到它被销毁—有两条路可走:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class ExampleService extends Service {
int mStartMode; // 表明在service被杀后的行为
IBinder mBinder; // 客户端绑定到的接口
boolean mAllowRebind; // 表明onRebind是否应被使用
@Override
public void onCreate() {
// The service is being created
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// service 正在启动,在调用startService()期间被调用
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// 一个客户端通过bindService()绑定到这个service
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// 所有的客户端使用unbindService()解除了绑定
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
// 一个客户端在调用onUnbind()之后,正使用bindService()绑定到service
}
@Override
public void onDestroy() {
// service不再被使用并将被销毁
}
}
注:不像activity的生命期回调方法们,你不需要调用父类的相应实现