Android中的四大组件之三:Service生命周期说明

在android系统开发中,Service类似于Windows的服务,运行在后台程序中,编写的自定义类需要继承Service类。
共有几个方法,oncreate(),onstart(),ondestroy()等方法。
onCreate():在服务启动时执行,只执行一次。
onStart():启动时执行,每次点击按钮都会执行。
onDestroy():停止服务时执行。

开启和停止服务的类:
启动服务的方法,startService();
停止服务的方法,stopService();

使用Intent启动服务的使用:
Intent intent = new Intent(this,MyService.class):
第一个参数传递上下文,第二个参数传递服务的class。

猜你喜欢

转载自chenxizhongjike.iteye.com/blog/2103913