Android IntentService详解及使用实例

 更新时间:2017年03月01日 16:47:56   投稿:lqh  
这篇文章主要介绍了Android IntentService详解及使用实例的相关资料,需要的朋友可以参考下

Android IntentService详解

一、IntentService简介

IntentService是Service的子类,比普通的Service增加了额外的功能。先看Service本身存在两个问题: 

  • Service不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中; 
  • Service也不是专门一条新线程,因此不应该在Service中直接处理耗时的任务;  

二、IntentService特征

  • 会创建独立的worker线程来处理所有的Intent请求; 
  • 会创建独立的worker线程来处理onHandleIntent()方法实现的代码,无需处理多线程问题; 
  • 所有请求处理完成后,IntentService会自动停止,无需调用stopSelf()方法停止Service; 
  • 为Service的onBind()提供默认实现,返回null; 
  • 为Service的onStartCommand提供默认实现,将请求Intent添加到队列中; 

 三、使用步骤(详情参考Service项目)

继承IntentService类,并重写onHandleIntent()方法即可;

MainActivity.Java文件

public class MainActivity extends Activity {  
  
  @Override  
  protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
  }  
  
  public void startService(View source) {  
    // 创建所需要启动的Service的Intent  
    Intent intent = new Intent(this, MyService.class);  
    startService(intent);  
  }  
  
  public void startIntentService(View source) {  
    // 创建需要启动的IntentService的Intent  
    Intent intent = new Intent(this, MyIntentService.class);  
    startService(intent);  
  }  
}  

 MyIntentService.java文件

public class MyIntentService extends IntentService {  
  
  public MyIntentService() {  
    super("MyIntentService");  
  }  
  
  @Override  
  protected void onHandleIntent(Intent intent) {  
    // IntentService会使用单独的线程来执行该方法的代码  
    // 该方法内执行耗时任务,比如下载文件,此处只是让线程等待20秒  
    long endTime = System.currentTimeMillis() + 20 * 1000;  
    System.out.println("onStart");  
    while (System.currentTimeMillis() < endTime) {  
      synchronized (this) {  
        try {  
          wait(endTime - System.currentTimeMillis());  
        } catch (InterruptedException e) {  
          e.printStackTrace();  
        }  
      }  
    }  
    System.out.println("----耗时任务执行完成---");  
  }  
}  
 

MyService.java文件

public class MyService extends Service {  
  
  @Override  
  public IBinder onBind(Intent arg0) {  
    return null;  
  }  
  
  @Override  
  public int onStartCommand(Intent intent, int flags, int startId) {  
    // 该方法内执行耗时任务可能导致ANR(Application Not Responding)异常  
    long endTime = System.currentTimeMillis() + 20 * 1000;  
    System.out.println("onStart");  
    while (System.currentTimeMillis() < endTime) {  
      synchronized (this) {  
        try {  
          wait(endTime - System.currentTimeMillis());  
        } catch (InterruptedException e) {  
          e.printStackTrace();  
        }  
      }  
    }  
    System.out.println("----耗时任务执行完成---");  
    return START_STICKY;  
  }  
}  

运行上述代码,启动MyIntentService的会使用单独的worker线程,因此不会阻塞前台的UI线程;而MyService会。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • android采用FFmpeg实现音视频合成与分离

    android采用FFmpeg实现音视频合成与分离

    这篇文章主要为大家详细介绍了android采用FFmpeg实现音视频合成与分离,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android实现带签到赢积分功能的日历

    Android实现带签到赢积分功能的日历

    这篇文章主要为大家详细介绍了Android实现带签到赢积分功能的日历,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Kotlin空安全空类型浅谈

    Kotlin空安全空类型浅谈

    这篇文章主要为大家介绍了Kotlin空安全空类型的实用技巧浅谈,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Android如何为按键添加声音

    Android如何为按键添加声音

    这篇文章主要告诉大家Android为按键添加声音的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • android 设置圆角图片实现代码

    android 设置圆角图片实现代码

    在android应用开发中,可能是美化需要,图片需要处理成圆角,本文将给出实现代码,开发中的遇到此问题的朋友可以参考下
    2012-11-11
  • Android自定义View实现动画效果详解

    Android自定义View实现动画效果详解

    这篇文章主要为大家详细介绍了Android如何通过自定义View实现动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-02-02
  • Android基础之获取LinearLayout的宽高

    Android基础之获取LinearLayout的宽高

    LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失。有的时候,我们需要想获取LinearLayout宽高,下面通过这篇文章来跟着小编一起学习学习吧。
    2016-11-11
  • Android开发中ViewPager实现多页面切换效果

    Android开发中ViewPager实现多页面切换效果

    ViewPager用于实现多页面的切换效果,该类存在于Google的兼容包里面,所以在引用时记得在BuilldPath中加入“Android-support-v4.jar”。具体详情大家可以参考下本文
    2016-11-11
  • Android常见XML转义字符(总结)

    Android常见XML转义字符(总结)

    下面小编就为大家带来一篇Android常见XML转义字符(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 使用PlatformView将 Android 控件view制作成Flutter插件

    使用PlatformView将 Android 控件view制作成Flutter插件

    这篇文章主要为大家介绍了使用PlatformView将 Android 控件view制作成Flutter插件实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11

最新评论