Service 不是一个单独的进程,也不是一个线程(和应用程序运行在同一个进程当中) 1.编写一个继承Service类的类,复写如下方法: ·onBind( ){ } ·onCreate( ){ } ·onStartCommand( ){ } ·onDestory( ){ } 2.在AndroidManifest.xml文件中注册service <service android:name=".类名"/> 3.启动Service(在Activity中) Intent intent = new Intent(); intent.setClass(Activity.this,MyService.class); startService(intent); //启动Service stopService(intent); //关闭Service
|