标签:
本篇的内容并不是介绍service使用方法和生命周期的,而是对其中的一些要点进行记录和分析。
我们都知道,Service是一个在后台执行的应用组件,用于在后台进行长期操作,例如进行网络事务,播放背景音乐等等。它又两种启动方式,如果其他的应用组件希望与该service进程内通信(IPC),可以通过与该service绑定来实现。关于生命周期的问题就不啰嗦了,直接放官档的图:
无论何种方式启动service,任何应用组件都可以使用该service。如果希望只有你自己的app才能使用该service,那么需要在manifest文件中加入android:exported
属性,并将其设置为false。Android官方建议我们尽量使用显式的方式来启动service,从而保证app的安全。
另外,我们需要注意的是,在默认情况下service并不会重新开启一个独立的线程运行,而是运行于宿主进程的主线程中。显然,如果我们需要进行耗时操作,例如上面提到的播放背景音乐,网络操作等等,该如何解决?
IntentService
当我们不需要同时处理多个请求的情况下,我们最好的解决方案是使用IntentService完成任务,而我们只需重写onHandleIntent()来完成处理逻辑即可。在IntentService进行了以下工作:
下面是一个IntentService的示例:
public class HelloIntentService extends IntentService { /** * A constructor is required, and must call the super IntentService(String) * constructor with a name for the worker thread. */ public HelloIntentService() { super("HelloIntentService"); } /** * The IntentService calls this method from the default worker thread with * the intent that started the service. When this method returns, IntentService * stops the service, as appropriate. */ @Override protected void onHandleIntent(Intent intent) { // Normally we would do some work here, like download a file. // For our sample, we just sleep for 5 seconds. long endTime = System.currentTimeMillis() + 5*1000; while (System.currentTimeMillis() < endTime) { synchronized (this) { try { wait(endTime - System.currentTimeMillis()); } catch (Exception e) { } } } } }
这里只需实现一个简单的构造函数和intent处理方法就可以了。当然,我们也可以根据需要重写service生命周期中的其他方法,但是请记得调用父类方法。我们需要清楚的是,所有提交给该service的任务都以intent的形式存放于一个队列中,IntentService里唯一的worker线程每次取一个intent来处理,都处理完后自动关闭IntentService。
如果希望多线程并发处理任务,那么需要继承service来处理任务了。这里就不演示具体代码了,但是有一点需要提醒一下。在onStartCommand方法会返回一个int值,通常我们返回父类的方法,这里的int值描述了当系统干掉service后的操作,它的几个常量选项如下:
另外,service还常常与notification相结合使用,使service可以运行到前台,例如音乐播放任务等等,可以与用户交互,这些内容以后再介绍。
标签:
原文地址:http://www.cnblogs.com/cqumonk/p/4745707.html