`
bcyy
  • 浏览: 1823280 次
文章分类
社区版块
存档分类
最新评论

Android Service---继承IntentService类

 
阅读更多
因为大多被启动类型的服务不需要同时处理多个请求(这实际是一个危险的多线程场景),因此使用IntentService类来实现自己的服务可能是最好的。

IntentService类执行以下操作:

1.创建一个独立与应用程序主线程的默认工作线程,执行所有的给onStartCommand()方法Intent的处理;

2.创建一个工作队列,以便每次只给你的onHandleIntent()方法实现传递一个Intent,因此你不必担心多线程的问题;

3.所有的启动请求都被处理之后终止这个服务,因此你不需要自己去调用stopSelf()方法;

4.提供返回null的onBind()方法的默认实现;

5.提供一个给工作队列发送Intent对象的onStartCommand()方法的默认实现和onHandleIntent()方法的实现。

所以这些加起来实际上只需要实现onHandleIntent()方法,来完成由客户提供的工作(虽然,你还需要给服务提供一个小的构造器)。

例如:

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) {
              }
          }
      }
  }
}

以上就是你做的全部:一个构造器和一个onHandleIntent()方法的实现。

如果你还决定要重写其他的回调方法,如onCreate()、onStartCommand()、或onDestroy(),那么就要确保调用父类的实现,以便IntentService对象能够适当的处理工作线程的活动。

例如,onStartCommand()方法必须返回默认的实现(默认的实现获取Intent并把它交付给onHandleIntent()方法):

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
    return super.onStartCommand(intent,flags,startId);
}

除了onHandleIntent()方法以外,唯一不需要调用实现的方法是onBind()方法(但是如果你的服务允许绑定,就要实现这个方法)。

注:本人转载系个人觉得翻译的很好,值得收藏,且自己回头看着方便。

如有兴趣请访问作者官方博客http://blog.csdn.net/FireOfStar

分享到:
评论

相关推荐

    Android中IntentService的特征

    service中1需要手动开启子线程2服务开启之后会一直运行,需要手动调用stopService();或者stopSelf(); intentService是一种异步(子线程)、自动停止的服务,这个例子测试IntentService的特征

    Android代码-Android-X5WebView基本封装

    Android-X5WebView基本封装和使用 通过OkHttp拦截器、自定义CookieJar有效完成客户端与H5端的Cookie同步管理 监听WebView的加载进度 滚动条的设置(隐藏或者显示,内侧显示还是外侧显示) 优化X5WebView的预加载问题...

    service和Intentservice示例

    介绍service和intentservice的使用和其中应该注意的部分

    Android代码-Android_Blog_Demos

    Android_Blog_Demos 存储CSDN博客的一些源码...Android IntentService完全解析 当Service遇到Handler 详细 Android 高清加载巨图方案 拒绝压缩图片 ViewDragHelper实战 自己打造Drawerlayout Android UI性能优化实

    android service和intentService

    3. 在ServiceDemoActivity.java中都调用了两个service,调用service自行屏蔽调用IntentServiceServie,调用IntentServiceServie自行屏蔽调用service。 4. 仅仅是个例子,对比这个service和IntentServiceServie的区别...

    Android代码-kotlin-android-examples

    Kotlin-Android-Examples This repository contains all the example apps demonstrating features/integrations of different libraries/functionality in your android apps purely developed on Kotlin. ...

    android 中的服务Service intentService例子

    NULL 博文链接:https://zhanglimijavaeye.iteye.com/blog/1881599

    Android IntentService详解及使用实例

    Android IntentService详解 一、IntentService简介 IntentService是Service的子类,比普通的Service增加了额外的功能。先看Service本身存在两个问题:  Service不会专门启动一条单独的进程,Service与它所在...

    Android—IntentService

    实现连接IntentService,打印输出日志

    深入剖析Android系统中Service和IntentService的区别

    主要介绍了Android系统中Service和IntentService的区别,与普通的服务相比,IntentService可以开启单独的线程来处理intent请求,需要的朋友可以参考下

    android IntentService 的学习例子

    自己写的一个android IntentService 的应用的例子,希望对大家有帮助。

    安卓 开启service每分钟执行一次任务 模拟定时 或者定时任务

    再开始之前我们还是先介绍下service吧:此处用的是IntentService,至于和常规的service有什么区别呢? 有了Service为什么还要有个IntentService呢? 原因如下: 1)Service默认运行在主线程中,IntentService运行在一个...

    详解Android中IntentService的使用方法

    Android中的IntentService是继承自Service类的,在我们讨论IntentService之前,我们先想一下Service的特点: Service的回调方法(onCreate、onStartCommand、onBind、onDestroy)都是运行在主线程中的。当我们通过start...

    IntentService

    IntentService基本使用方法Demo

    IntentService模拟上传图片

    对于有写工作,我们需要在后台进行,这里主要介绍了使用IntentService轻松实现模拟上传图片的例子。对于IntentServixce和Service扩展类的使用区别,大家可以自行去了解研究一下。

    IntentService简单应用

    Android 中IntentService的简单应用

    android-services-demo, 用于服务和通知的Android演示( 星期 4 ).zip

    android-services-demo, 用于服务和通知的Android演示( 星期 4 ) 服务演示这是一个用于服务和通知的Android演示,包括:使用 IntentService使用ResultReceiver在IntentService和 Activity 之间进行通信使用 ...

Global site tag (gtag.js) - Google Analytics