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)    @Override    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {        initNotification();        startForeground(notificaton_id, builder.build());        return super.onStartCommand(intent, flags, startId);    }    @Override    protected 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(){        @Override        public void run() {            super.run();            builder.setProgress(total,currentProgress,false);            manager.notify(notificaton_id,builder.build());            handler.postDelayed(nThread,1000);        }    };    @Override    public 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);    }

更多相关文章

  1. 如何在后台运行Linux命令?
  2. 一款常用的 Squid 日志分析工具
  3. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  4. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  5. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  6. 可勾选的ExpandableListView
  7. New Android(安卓)Project In Eclipse with SDK 4.0
  8. Ant 打包 Android(安卓)应用
  9. Android基础学习【历史流程重走】 ---- Android入门基础(二)

随机推荐

  1. Delphi XE5 android 获取网络状态《转》
  2. android 打开通知栏
  3. Android中利用Intent传递数据到另一个页
  4. Android之关于手势操作图片的缩放与移动
  5. android 调用 Menu 两种方法
  6. android三个选项的对话框
  7. Android(安卓)利用TwinklingRefreshLayou
  8. android更新UI的几种方法
  9. Android(安卓)RadioButton 单选按钮
  10. android中动态布局(动态加入TextView和Li