1.服务(Service)简介
Android的四大组件之一,其分类还是蛮多的,生命周期也与Activity的相近,还有与Thread的区别,有人已经做了很好的总结,大家不妨看下这个blog。
2.例子
这里介绍一种服务--通知中心通知,这种服务很常见,包括图标,标题,内容,时间这些信息,其特性是:做着自己的事儿,可供多个Activity控制。
2.1 MyService.java
public class MyService extends Service {
private final static String TAG = MyService.class.getSimpleName();
private NotificationManager notificationMgr; // 通知中心
private boolean canRun = true;
private IBinder binder = new MyBinder(); // 用于和外界交互
private int notifyId = 0x222;
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public void onCreate() {
Thread thread = new Thread(null, new ServiceRunnable(), "BackgroundService");
thread.start();
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, String.format("onBind, intent = %s", intent.toString()));
notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
displayNotificationMessage("服务已启动:onBind");
return binder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "start action = " + intent.getAction());
notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
displayNotificationMessage("服务已启动:onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
stopForeground(true);
canRun =false;
super.onDestroy();
}
//为服务设置图标和文字描述
private void displayNotificationMessage(String message) {
Notification notification = new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, "我的通知", message, contentIntent);
notificationMgr.notify(++notifyId, notification);
}
//ServiceWorker service自身的线程,用于做自己的事情,这里为了表示服务的确在运行,每2秒打印一次log信息。
class ServiceRunnable implements Runnable {
int counter = 0;
@Override
public void run() {
// do background processing here.....
while(canRun) {
Log.d(TAG, "counter:"+counter);
counter ++;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
2.2 MainActivity.java
public class MainActivity extends Activity {
private MyService myService;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
MyBinder myBinder = (MyBinder) service;
myService = myBinder.getService();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intentService = new Intent(MainActivity.this, MyService.class);
intentService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentService.setAction("dontes");
MainActivity.this.bindService(intentService, serviceConnection, BIND_AUTO_CREATE);
startService(intentService);
}
}
在MainAcitvity中使用了该服务,当然,不要忘了在AndroidManifest.xml中注册该Service.
<service android:name="com.servicetest.main.MyService" >
</service>