您的位置:首页 > 移动开发 > Android开发

Android使用IntentService执行异步任务

2016-08-15 23:07 555 查看
启动服务后,待服务执行完onHandleIntent 会自动退出

public class MyIntentService extends IntentService {

@Override
public void onDestroy() {
super.onDestroy();
Log.i("Service","MyIntentServiceDestroy");
}
public MyIntentService() {
super("MyIntentServiceName");
}
@Override
protected void onHandleIntent(Intent intent) {
try{
int result = DownloadFile(new URL("http://www.x-motion.com/somefile.pdf"));
Log.i("Service","Download "+result +" bytes");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
private int DownloadFile(URL url){
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 100;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 异步