EventBus服务下载使用

2019-07-19  本文已影响0人  玖玖君
 public class DownLoadService extends Service {
   
    @Override
    public void onCreate() {
        super.onCreate();
        //请求数据
        okDownLoad();
    }

  @Override
   onStartCommand ···{}

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    //执行请求
    private void okDownLoad() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .build();

        Request request = new Request.Builder()
                .get()
                .url("http://cdn.banmi.com/banmiapp/apk/banmi_330.apk")
                .build();
        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("tag","onFailure:"+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                InputStream inputStream = body.byteStream();
                long length = body.contentLength();

                saveFile(inputStream,length, Environment.getExternalStorageDirectory()+"/fw.apk");

            }
        });
    }

//执行操作
    private void saveFile(InputStream inputStream, long length, String path) {

        try {
            FileOutputStream outputStream = new FileOutputStream(new File(path));
            int len=0;
            int count=0;
            byte[] bytes = new byte[1024 * 20];
            while ((len=inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
                count+=len;

                Log.e("TAG", "当前进度:" + count + " / " + length);

                //MessageEvent是创的一个Bean包,里面放要传递的数据

                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setContentLength(length);
                messageEvent.setCount(count);
                //发送数据

                EventBus.getDefault().post(messageEvent);
            }
            inputStream.close();
            outputStream.close();
            Log.e("tag","下载完成");

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        initView();
    }

  //在此方法中来执行EventBus的命令实现操作(这是主线程)
 @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(MessageEvent messageEvent) {
        long contentLength = messageEvent.getContentLength();
        long count = messageEvent.getCount();
        //设置进度
        mPb.setMax((int) contentLength);
        mPb.setProgress((int) count);
        mTvPb.setText((int) count * 100 / contentLength + "%");
    }


    @Override
    protected void onDestroy() {
       //解注册EventBus
        EventBus.getDefault().unregister(this);
       //解绑服务  
      stopService(new Intent(this,DownLoadService.class));
       //停止应用  
       finish();
    }

上一篇 下一篇

猜你喜欢

热点阅读