Service学习——第五篇 使用IntentService在单独线程上执行异步任务
2014-09-21 14:57
507 查看
Service学习——使用IntentService在单独的线程上执行异步任务
特点:它可以在任务执行完成后自动结束服务。
Service类需要我们手动去终止,但是有很多时候我们忘记了终止它,或者不知道什么时候去终止,这时IntentService我感觉就会很好用。
使用方法:
首先,定义一个MyIntentService类来继承IntentService
接着,实现onHandleIntent()方法
代码如下:
package com.example.myservice;
import java.net.URL;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyIntentService extendsIntentService {
publicMyIntentService(String name) {
super(name);
//TODO Auto-generated constructor stub
}
publicMyIntentService(){
super("MyIntentService");
}
@Override
protectedvoid onHandleIntent(Intent intent) {
//TODO Auto-generated method stub
try{
intresult = DownloadFile(new URL("http://www.baidu.com"));
Log.d("IntentService","Download" + result + "bytes");
}catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
}
}
privateint DownloadFile(URL url){
try{
Thread.sleep(5000);
}catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return100;
}
}
别忘记在Manifest中声明这个服务:
<serviceandroid:name="com.example.myservice.MyIntentService"></service>
改动MainActivity:
Intent intent = new Intent(MainActivity.this,MyIntentService.class);
startService(intent);
运行这个程序,大概5秒钟,你会在LogCat中看到运行结果:Download 100bytes
特点:它可以在任务执行完成后自动结束服务。
Service类需要我们手动去终止,但是有很多时候我们忘记了终止它,或者不知道什么时候去终止,这时IntentService我感觉就会很好用。
使用方法:
首先,定义一个MyIntentService类来继承IntentService
接着,实现onHandleIntent()方法
代码如下:
package com.example.myservice;
import java.net.URL;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyIntentService extendsIntentService {
publicMyIntentService(String name) {
super(name);
//TODO Auto-generated constructor stub
}
publicMyIntentService(){
super("MyIntentService");
}
@Override
protectedvoid onHandleIntent(Intent intent) {
//TODO Auto-generated method stub
try{
intresult = DownloadFile(new URL("http://www.baidu.com"));
Log.d("IntentService","Download" + result + "bytes");
}catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
}
}
privateint DownloadFile(URL url){
try{
Thread.sleep(5000);
}catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return100;
}
}
别忘记在Manifest中声明这个服务:
<serviceandroid:name="com.example.myservice.MyIntentService"></service>
改动MainActivity:
Intent intent = new Intent(MainActivity.this,MyIntentService.class);
startService(intent);
运行这个程序,大概5秒钟,你会在LogCat中看到运行结果:Download 100bytes
相关文章推荐
- Android使用IntentService执行异步任务
- 线程相关——HandlerThread、IntentService、ResultReceiver:结果接收者、AsyncTask:异步任务、Android中处理线程间通信的方式
- 使用IntentService执行后台任务
- Android中使用Handler和异步任务(AsyncTack)来为UI线程执行费时操作
- 使用 Task 替换 ThreadPool ,异步监测所有线程(任务)是否全部执行完毕
- Android Training - 使用IntentService执行任务(Lesson 3 - 回传任务状态给发送方)
- Android Training - 使用IntentService执行任务(Lesson 1 - 创建IntentService)
- Android中使用IntentService执行后台任务
- Service学习——第三篇 异步执行任务
- 使用java.util.concurrent.ExecutorService实现Java线程异步执行
- Android使用IntentService执行耗时任务
- C# 使用 Task 替换 ThreadPool ,异步监测所有线程(任务)是否全部执行完毕
- Android中使用Handler和异步任务(AsyncTack)来为UI线程执行费时操作
- 【Android 开发教程】使用IntentService执行异步操作
- Android基础学习之异步任务使用Thread(线程)
- Android中使用IntentService执行后台任务
- Android Training - 使用IntentService执行任务(Lesson 2 - 发送任务给IntentService)
- Android中AsyncTask(异步执行任务)的使用
- C# 线程知识--使用Task执行异步操作(转)
- Service-IntentService集成异步线程(2)