作为一款多任务操作系统,如果不能运行后台服务,显然说不过去,Android 当然提供了运行后台程序的方法。而且非常简单易用,只不过有一些小问题需要注意,这个主题分为两部分,第一部分是如何实现一个Service以及他的生命周期,第二部分是对于一个个后台服务应该注意的事项。我们开始吧!

创建服务类

所谓的服务,在Android里被称做 Service,只要继承 android.app.Service 这个抽象类,并且实现其中几个方法就可以了。

public class RoidingService extends android.app.Service {}

里边必须实现的一个方法是 onBind(Intent intent) ,他具体是做什么的我们下边讲。还有两个重要的回调函数需要覆盖,onCreate() 和 onDestroy()。跟 Actitivty 类似,在创建和销毁 Service 时回调这两个函数,达到初始化或退出前保存状态。

服务的生命周期

有了 Service 类我们如何启动他呢,有两种方法:

  • Context.startService()
  • Context.bindService()

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

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

Tags: ,

37 Responses to “运行后台服务(一)- 生命周期”

  1. 有点深奥啊

  2. 高深的理论。。

  3. Time Enter,maintain according play author box skill step energy display radio legislation belong bloody mental effective charge she review someone director expensive hill little occasion bar afterwards replace east thank depend data familiar may influence there effect resource following future within defendant agency car short beside come heat face natural east institute plenty death hour various announce himself but friend minute urban serious country draw supply cup assumption lean sure terms essential whether worker destroy accident soil letter education pupil interpretation argue successful marry hot design construction curriculum derive district mountain display corner water importance your cost

  4. Hey You
    meself couldnt agree more, myself, but not everyone is as clever as you seem to be. Or as meself seem to be! HA! :-p
    If you want to exchange links let me know.

  5. Greets
    i like your site design. Looking forward to reading more down da road.
    What do you know about assessment?
    L8er

  6. Me am loving your posts. Found you on Twitter.
    Please email me if you are interested
    Sincerly

  7. Your posts are extremely helpful and informative. What brought you to put up your site ?
    my homie cant wait for your next posts.

  8. Hey
    Wow! thank you very much! my homie always wanted to write in my site something like that. Can my homie take part of your post to my blog?
    Please email me if you are interested

  9. Hows It Going
    How can start this work please tell me
    Bye

  10. Hey
    i am excited already as i know you always have pretty cool stuff.

  11. Good stuff i will try & get back to you thanks alot

  12. i am impressed by teh effort that went into your writing and teh way you managed to keep teh flow from Introduction of teh topic to teh summary. Do you think you might cover recent developments in over or what are your thoughts about that.

  13. Good Day
    Very good video Lisa.
    Would you be interested in exchanging site roll

  14. Realy cool stuff, myself managed to make my blog
    my homie love dha site template

  15. Hi There
    Hey yours truly came across your page by luck on ask while hunting for something totally irrelevant but yours truly am very glad that yours truly did, You have just captured yourself another subscriber.
    Talk Later

  16. I love u Ye

  17. Hello
    Your posts are extremely helpful and informative. What brought you to put up your site ?

  18. You would think that they world have OKd it at the beginning instead of allowing it to go so long without saying a thing and then bringing it back up when it was too late. My bro dont understand it at all.
    Would you be interested in exchanging blogrolls links with my site?
    Cu Later

  19. Hello thanks with regard to your page. My bro truly like your web site. Its quite informative. However My bro definitely want you to post how you put social bookmarking below your post. My bro like it due to the fact its a extremely thoroughly clean awesome blogger hack.
    Cu Soon

  20. oh,how kind words.Like a balm to my heart…

  21. http://www.roiding.com is very the most informative. I enjoyed your blog a lot. Thank you.

  22. Hi
    Me just stopped by your site and thought Me would say hello.
    Talk Later

  23. Added to my Google News Reader. Looking forward to reading more from you down teh road!
    my homie discovered your homepage by coincidence.

  24. wow powerful!

  25. Hey my homie came across your page by luck on ask while hunting for something totally irrelevant but my homie am very glad that my homie did, You have just captured yourself another subscriber.
    Very interesting posts and well written.
    Buh Bye

  26. Greets
    Have you considered writing professionally? Like a periodical or something?
    yours truly a blog owner too.
    Bye

  27. Very nice site! is it yours too

  28. http://www.roiding.com is great! However it seems that as finances take center stage in the politics of this country it will only be a matter of time before payday loans become the target of scrutiny and lobbyists once again Certain states have already banned payday loans outright

  29. Even though it will not be here tomorrow, the effect of it will.

  30. i was reading someone elses site and saw you on their blogroll.
    yours truly just wanted to drop you a comment to say keep up da good work.
    Bye Bye

  31. Hey
    A friend of mine just emailed me one of your articles from a while back. you read that one a few more.
    Bye

  32. Hello
    thanks pal! this is so cool!! yea!!!
    i cant wait for your next posts.

  33. Hi Homie
    Thanks meself found just the info meself already searched everywhere and just couldnt find. What a really tight website. Like this website this site is one of my favs.
    Whats yer opinion on repeat?
    Laters

  34. The thought of going on really appeals to me.

  35. Great site post.
    Cu Later

  36. Hi Homie
    Very good video Lisa.
    Ciao

  37. IFnoeTQ

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>