Notificaton+IntentService下载
Notificaton+IntentService下载下载音乐
- 思路
- 1.后台下载用IntentService
- 2.获得文件总长度
- 3.每隔多长时间发送通知
- 详细代码
- Service.java
- Activity.java
思路
1.后台下载用IntentService
2.获得文件总长度
3.每隔多长时间发送通知
详细代码
Service.java
public class DownLoadMusicService extends IntentService {public InputStream inputStream;public int total;public NotificationManager manager;public int notificaton_id = 100;public NotificationCompat.Builder builder;public Handler handler = new Handler();public int currentProgress = 0;HttpURLConnection connection;public DownLoadMusicService() {super("LoadMusic");}@RequiresApi(api = Build.VERSION_CODES.O)@Overridepublic int onStartCommand(@Nullable Intent intent, int flags, int startId) {initNotification();startForeground(notificaton_id, builder.build());return super.onStartCommand(intent, flags, startId);}@Overrideprotected void onHandleIntent( @Nullable Intent intent) {String path = intent.getStringExtra("url");try {URL url = new URL(path);connection = (HttpURLConnection) url.openConnection();connection.setRequestProperty("Accept-Encoding", "identity");//不加此行得不到总长度total = connection.getContentLength();//拿不到if(connection.getResponseCode() == 200){handler.post(nThread);inputStream = connection.getInputStream();int len = 0;byte[] bytes = new byte[60];File file0 = Environment.getExternalStorageDirectory();File newFile = new File(file0,"dabeizhou.mp3");FileOutputStream fileOutputStream = new FileOutputStream(newFile);while ((len = inputStream.read(bytes)) != -1){currentProgress+=len;fileOutputStream.write(bytes,0,len);}Log.d("amy",currentProgress+"--");}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {if(inputStream != null)inputStream.close();} catch (IOException e) {e.printStackTrace();}if(connection != null)connection.disconnect();}}@RequiresApi(api = Build.VERSION_CODES.O)public void initNotification(){manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationChannel channel = new NotificationChannel("channel_1","name",NotificationManager.IMPORTANCE_LOW);manager.createNotificationChannel(channel);builder = new NotificationCompat.Builder(this,"channel_1").setSmallIcon(R.mipmap.bq1).setContentText("下载文件").setContentText("下载中。。");}/*** 线程用来发送通知*/Thread nThread = new Thread(){@Overridepublic void run() {super.run();builder.setProgress(total,currentProgress,false);manager.notify(notificaton_id,builder.build());handler.postDelayed(nThread,1000);}};@Overridepublic void onDestroy() {super.onDestroy();handler.removeCallbacks(nThread);}
}
Activity.java
public void downloadMusic(View view) {//下载Intent intent = new Intent(this,DownLoadMusicService.class);intent.putExtra("url","http://m10.music.126.net/20200704095418/39a88357cd32cb186ccaed3699cd2083/ymusic/8481/cbba/7868/c96fe64c4a34c2137ce30e5f941b145c.mp3");startService(intent);}
Notificaton+IntentService下载相关推荐
- 安卓Service组件使用系列3:使用IntentService下载网络图片
使用IntentService的特点: 1.不需要开启线程 2.不需要关闭服务,自己会关闭 3.使用单线程下载数据 (不需要完成线程的编写,直接访问网络就可以获取图片) 因为以上两点都在 ...
- 外企邮件回复模板_电子邮件回复模板
外企邮件回复模板 If you frequently type the same responses to email questions, you can use an email response ...
- android教学大纲
android班 教学大纲 每次课4个小时 第1次课:Android开发环境搭建及工具介绍 Android系统简介 Android开发环境简介及搭建 AndroidStudio提供的工具组件 第2次课 ...
- iphone开发笔记和技巧总结
在iphone程序中实现截屏的一种方法: //导入头文件 #importQuartzCore/QuartzCore.h //将整个self.view大小的图层形式创建一张图片imageUIGrap ...
- 用URL在IntentService中下载图片并更新到ImageView
整个流程还是采用的之前的逻辑,即在Activity触发事件,交给IntentService去处理,并将处理的结果用broadcastReceiver发给Activity内的broadcastRecei ...
- Service IntentService区别 (面试)
依然记得自己当初没有真正的工作经验的时候的日子,满北京跑,没有人要.妈的,现在就想问,还有谁!想想真解气.不提了. 曾经有个面试官问我service 和IntentService的区别.当时自己模模糊 ...
- 在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast
在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast 1. 表象 Service中可以正常显示Toast,IntentServic ...
- 使用IntentService给自己的Android应用写一个文件下载器。
接着上一篇的http://www.cnblogs.com/zhengxt/p/3657833.html,当我们想给自己的APP写一个文件下载器时,可以用重写IntentService来实现. 使用In ...
- android后台文件下载库,android中如何下载文件并显示下载进度
最近开发中遇到需要下载文件的问题,对于一般的下载来说不用考虑断点续传,不用考虑多个线程,比如下载一个apk之类的,这篇文章讨论的就是这种情形. 这里主要讨论三种方式:AsyncTask.Service ...
最新文章
- Oracle RMAN 还原与恢复(一)--还原服务器参数文件
- 【python教程】IO文件操作的常见用法
- hive中如何进行按周计算
- Python学习笔记(二)——高级特性
- sqL编程篇(三) 游标与存储过程
- SpringMVC转发和重定向
- OpenGait:首个步态识别框架开源了!
- 摄影平铺海报psd模板|简单搭建层次场景海报
- SQL SERVER 中的*=和=*
- [工具] PicGo + Github 图床
- MATLAB函数fir1
- Layabox的2d精灵的性能优化
- 计算机提示无法访问手机tf卡,电脑(手机)无法读取内存卡(存储卡)的解决方法大全...
- 群内2018_4月讨论整理2
- 利用爬虫将Yuan先生的博客文章爬取下来
- 【SAP Abap】SAP系统数据快速导出
- Martin Fowler先生关于持续集成的一篇文章
- android圆形图标显示不全,android 桌面图标显示不全
- php mcrypt_decrypt用法,mcrypt_decrypt PHP正确用法
- 通过MES系统如何提升生产车间生产效率?