WEB开发网
开发学院手机开发Android 开发 Android Service 笔记 阅读

Android Service 笔记

 2010-08-22 04:47:00 来源:WEB开发网   
核心提示:itself. In this mode, it's started by calling Context.startService() and stopped by calling Context.stopService(). It can stop itself by calling Service.sto
itself. In this mode, it's started by calling Context.startService() and stopped by calling Context.stopService(). It can stop itself by calling Service.stopSelf() or Service.stopSelfResult(). Only one stopService() call is needed to stop the service, no matter how many times startService() was called.

2、It can be operated programmatically using an interface that it defines and exports. Clients establish a connection to the Service object and use that connection to call into the service. The connection is established by calling Context.bindService(), and is closed by calling Context.unbindService(). Multiple clients can bind to the same service. If the service has not already been launched, bindService() can optionally launch it.

也可以在startService()后bindService()。

你应该实现Service的方法( they are public):

void onCreate()

void onStart(Intent intent)

void onDestroy()

通过实现这些方法,你可以监视Service生命周期中的两个嵌套循环:

1、Service的完全生命时间(entire lifetime)是指从调用onCreate()开始到onDestroy()返回的这段时间。Service在onCreate()中进行初始化,在onDestroy()中释放资源。

2、Service的活动生命时间(active lifetime)从onStart()开始,onStart()会处理从startService()方法传递过来的Intent对象。

Service不存在onStop()方法。

注意:只有通过startService()启动Service才会调用它的onStart()方法,通过onBind()启动的Service不会调用。

The onCreate() and onDestroy() methods are called for all services, whether they're started by Context.startService() or Context.bindService(). However, onStart() is called only for services started by startService().

如果Service运气其他程序bind到它,你需要实现其他的回调方法。

IBinder onBind(Intent intent)

boolean onUnbind(Intent intent)

void onRebind(Intent intent)

传递给bindService()的Intent对象会传递给onBind(),传递给unbindService()的Intent对象会传递给

上一页  1 2 3 4 5 6 7  下一页

Tags:Android Service 笔记

编辑录入:coldstar [复制链接] [打 印]
赞助商链接