Android Application 里面怎么启动service

Android Application 里面怎么启动service

1.

       Intent intent = new Intent(A.this,Service.class);  
       startService(intent);  
    在同一个应用任何地方调用 startService() 方法就能启动 Service 了,然后系统会回调 Service 类的 onCreate() 以及 onStart() 方法。这样启动的 Service 会一直运行在后台,直到 Context.stopService() 或者 selfStop() 方法被调用。另外如果一个 Service 已经被启动,其他代码再试图调用 startService() 方法,是不会执行 onCreate() 的,但会重新执行一次 onStart() 。

2.

        Intent intent = new Intent(A.this,Service.class);  
        ServiceConnection conn = new ServiceConnection(){//逻辑操作};
        bindService(intent, conn, Context.BIND_AUTO_CREATE); 
    bindService() 方法的意思是,把这个 Service 和调用 Service 的客户类绑起来,如果调用这个客户类被销毁,Service 也会被销毁。用这个方法的一个好处是,bindService() 方法执行后 Service 会回调上边提到的 onBind() 方发,你可以从这里返回一个实现了 IBind 接口的类,在客户端操作这个类就能和这个服务通信了,比如得到 Service 运行的状态或其他操作。如果 Service 还没有运行,使用这个方法启动 Service 就会 onCreate() 方法而不会调用 onStart()。

区别概况为:
startService() 的调用者与服务没有联系,即使调用者退出了,服务仍然运行,而bindService() 的调用者与服务绑在一起,调用者一旦退出了,服务也随即终止掉。

跟你在activity里面差不多。。
只不过传入的是**getApplicationContext()**

startService(new Intent(getApplicationContext(),xxxxx.class))

参考:

http://www.cnblogs.com/yejiurui/p/3429451.html

http://blog.csdn.net/liyuanjinglyj/article/details/46897231
看文章后面,有详细说明。