Java tutorial
/* * Copyright 2016 jeasonlzy(?) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.lzy.demo.okgo; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.text.format.Formatter; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.lzy.demo.R; import com.lzy.demo.base.BaseDetailActivity; import com.lzy.demo.ui.NumberProgressBar; import com.lzy.demo.utils.Urls; import com.lzy.okgo.OkGo; import com.lzy.okgo.callback.FileCallback; import com.lzy.okgo.model.Progress; import com.lzy.okgo.model.Response; import com.lzy.okgo.request.base.Request; import java.io.File; import java.text.NumberFormat; import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; /** * ================================================ * jeasonlzy?Github?https://github.com/jeasonlzy * 1.0 * 16/9/11 * ?? * ? * ================================================ */ public class SimpleDownloadActivity extends BaseDetailActivity { private static final int REQUEST_PERMISSION_STORAGE = 0x01; @Bind(R.id.fileDownload) Button btnFileDownload; @Bind(R.id.downloadSize) TextView tvDownloadSize; @Bind(R.id.tvProgress) TextView tvProgress; @Bind(R.id.netSpeed) TextView tvNetSpeed; @Bind(R.id.pbProgress) NumberProgressBar pbProgress; private NumberFormat numberFormat; @Override protected void onActivityCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_file_download); ButterKnife.bind(this); setTitle("?"); numberFormat = NumberFormat.getPercentInstance(); numberFormat.setMinimumFractionDigits(2); checkSDCardPermission(); } /** SD??? */ protected void checkSDCardPermission() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_PERMISSION_STORAGE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_PERMISSION_STORAGE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //??? } else { showToast("????"); } } } @Override protected void onDestroy() { super.onDestroy(); //Activity?? OkGo.getInstance().cancelTag(this); } @OnClick(R.id.fileDownload) public void fileDownload(View view) { OkGo.<File>get(Urls.URL_DOWNLOAD)// .tag(this)// .headers("header1", "headerValue1")// .params("param1", "paramValue1")// .execute(new FileCallback("OkGo.apk") { @Override public void onStart(Request<File, ? extends Request> request) { btnFileDownload.setText(""); } @Override public void onSuccess(Response<File> response) { handleResponse(response); btnFileDownload.setText("?"); } @Override public void onError(Response<File> response) { handleError(response); btnFileDownload.setText(""); } @Override public void downloadProgress(Progress progress) { System.out.println(progress); String downloadLength = Formatter.formatFileSize(getApplicationContext(), progress.currentSize); String totalLength = Formatter.formatFileSize(getApplicationContext(), progress.totalSize); tvDownloadSize.setText(downloadLength + "/" + totalLength); String speed = Formatter.formatFileSize(getApplicationContext(), progress.speed); tvNetSpeed.setText(String.format("%s/s", speed)); tvProgress.setText(numberFormat.format(progress.fraction)); pbProgress.setMax(10000); pbProgress.setProgress((int) (progress.fraction * 10000)); } }); } }