IntentService是继承自Service实现了异步功能的类。
IntentService{
创建
![](https://i-blog.csdnimg.cn/blog_migrate/b77ef8c444fc046ed4ea8d7e6fdc096e.png)
开启服务
Intent intent = new Intent(this, MyIntentService.class);
startService(intent);
增加权限
![](https://i-blog.csdnimg.cn/blog_migrate/edb3463b1045e2c59c3dcb26be2b3a43.png)
前台服务
![](https://i-blog.csdnimg.cn/blog_migrate/1f2892ef93e9ec5cd522bc3c948fb8b9.png)
在创建的服务类接收数据
Bundle bundle = intent.getExtras();
String urlpath = bundle.getString("urlpath");
String sdpath = bundle.getString("sdpath");
读出sd卡的文件
try {
URL url = new URL(urlpath);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(2000);
httpURLConnection.setConnectTimeout(2000);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
if(httpURLConnection.getResponseCode()==200){
InputStream inputStream = httpURLConnection.getInputStream();
File file = new File(sdpath);
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
while ((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
发送通知+IntentService{
1.创建BroadCastReceiver
2.创建进度条通知
![](https://i-blog.csdnimg.cn/blog_migrate/f56d947a23eaf4b97e698ca3720b2ec5.png)
3.发送通知+进度条
![](https://i-blog.csdnimg.cn/blog_migrate/8cff303c63142912135f01934ce25b92.png)
4.接收通知
![](https://i-blog.csdnimg.cn/blog_migrate/f3d01ce9b6a5a0b64cac509b41561c5b.png)
清单文件注意事项
![](https://i-blog.csdnimg.cn/blog_migrate/d35cce9e4b29e327c0bb4c782248a344.png)
}