IntentService多线程
来源:互联网 发布:linux虚拟机和双系统 编辑:程序博客网 时间:2024/05/18 02:44
IntentService继承自Service,用于异步处理通过startService(Intent intent)方法传递的Intent对象。
该Service根据需要启动
,通过实现onHandleIntent(Intent intent)方法,IntentService会在一个工作线程中,
按顺序处理每个Intent对象,直到当工作执行完毕自动销毁。
实例代码
1、启动服务
- Intent intent = new Intent("iteye.dyingbleed.DownloadService");
- intent.putExtra("url", url); //添加下载地址
- startService(intent);
2、配置AndroidManifest
3、新建DownloadService类,继承IntentService
- package lizhen.apk;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import android.app.IntentService;
- import android.content.Intent;
- public class DownloadService extends IntentService {
- private ExecutorService pool;
- public DownloadService() {
- super("DownloadService");
- }
- @Override
- public void onCreate() {
- super.onCreate();
- pool = Executors.newCachedThreadPool();
- }
- @Override
- protected void onHandleIntent(Intent intent) {
- String url = intent.getStringExtra("url");
- pool.execute(new DownloadTask(url));
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- pool.shutdown();
- }
- private class DownloadTask implements Runnable {
- private final String url;
- public DownloadTask(String url) {
- this.url = url;
- }
- @Override
- public void run() {
- // TODO 此處省略具體實現
- }
- }
- }
0 0
- IntentService多线程
- Android多线程之IntentService
- Android多线程之IntentService
- Android 多线程之IntentService 完全详解
- Android多线程开发之IntentService的使用
- Android多线程:IntentService用法&源码分析
- Android多线程:IntentService用法&源码分析
- Android多线程编程技术之使用IntentService
- Android 多线程之IntentService 完全详解
- Android多线程--IntentService的使用及原理
- IntentService
- IntentService
- intentservice
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- [mysql]mac在命令行使用mysql
- Ubuntu 14.04输入法安装
- HashSet原理
- 最小生成树-kruskal模板
- sql中通配符
- IntentService多线程
- LA4329
- java 操作excel,jxl加边框,jxl合并单元格,单元格的设置,单元
- UVA - 639 Don't Get Rooked (回溯)
- JavaScript 基础
- 【最短路径】poj 1062
- Resource、drawable、bitmap 转换
- 上传资源勘误
- Java中断一个正在运行的线程