移动应用开发郑贵锋08 服务与多线程.ppt

移动应用开发郑贵锋08 服务与多线程

开启Service 在Activity中可以通过startService(Intent)开启一个Service。与Activity跳转类似。 Intent intent = new Intent(this,MyService.class); startService(intent); 其中MyService类是开发者自定义的继承Service的子类。 当第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法。若Service已经启动,当再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。 模式一:开始和停止Service 关闭Service 在Activity中通过stopService(Intent)关闭Service; Intent intent = new Intent(this,MyService.class); stopService(intent); 或者在Service中通过stopSelf()关闭自身。 模式一:开始和停止Service 通过Context.bindService()方法启动服务,通过 Context.unbindService()关闭服务。多个客户端可以绑定至同一个服务。如果服务此时还没有加载,b

文档评论(0)

1亿VIP精品文档

相关文档