菜鸟一只,写点文章,保证自己学到的东西不会忘记
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
}
网络功能采用OkHttp来实现
public interface DownloadListener {
void onProgress(int progress);
void onSuccess();
void onFailed();
void onPause();
void onCanceled();
}
一共定义5个回调方法,onProgress()方法用于通知当前的下载进度,onSuccess()方法用于通知下载完成事件,onFailed()用于通知下载失败事件,onPause()用于通知下载暂停事件,onCanceled()用于通知下载取消事件。
通过AsyncTask来实现下载功能
package com.slience.downloaddemo.downloadtask;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import com.slience.downloaddemo.downloadinterface.DownloadListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Slience on 2017/10/20.
*/
public class DownloadTask extends AsyncTask {
public static final int TYPE_SUCCESS = 0;
public static final int TYPE_FAILED = 1;
public static final int TYPE_PAUSED = 2;
public static final int TYPE_CANCELED = 3;
public static final String TAG = "DownloadTask";
private DownloadListener listener;
private boolean isCanceled = false;
private boolean isPaused = false;
private int lastProgress;
public DownloadTask(DownloadListener listener){
this.listener = listener;
}
@Override
protected Integer doInBackground(String... params) {
InputStream is = null;
RandomAccessFile savedFile = null;
File file = null;
try {
long downloadedLength = 0; //记录已下载的文件长度
String downloadUrl = params[0];
Log.i(TAG , "DownloadUrl = " + downloadUrl);
String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
Log.i(TAG , "FileName = " + fileName);
String dirctory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
Log.i(TAG , "Dirctory = " + dirctory);
file = new File(dirctory + fileName);
if(file.exists()){
downloadedLength = file.length();
}
long contentLength = getContentLength(downloadUrl);
Log.i(TAG , "ContentLength = " + contentLength);
if(contentLength == 0){
return TYPE_FAILED;
}else if(contentLength == downloadedLength){
return TYPE_SUCCESS;
}
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.addHeader("RANGE" , "bytes = " + downloadedLength + "-")
.url(downloadUrl)
.build();
Response response = client.newCall(request).execute();
if(response != null){
is = response.body().byteStream();
savedFile = new RandomAccessFile(file , "rw");
savedFile.seek(downloadedLength);
byte[] b = new byte[1024];
int total = 0;
int len;
while((len = is.read(b)) != -1){
if(isCanceled){
return TYPE_CANCELED;
}else if(isPaused){
return TYPE_PAUSED;
}else{
total += len;
savedFile.write(b , 0 , len);
//计算下载百分比
int progress = (int)((total + downloadedLength) * 100 / contentLength);
publishProgress(progress);
}
}
response.body().close();
return TYPE_SUCCESS;
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(is != null){
is.close();
}
if(savedFile != null){
savedFile.close();
}
if(isCanceled && file != null){
file.delete();
}
}catch (Exception e){
e.printStackTrace();
}
}
return TYPE_FAILED;
}
@Override
protected void onProgressUpdate(Integer... values) {
int progress = values[0];
if(progress > lastProgress){
listener.onProgress(progress);
lastProgress = progress;
}
}
@Override
protected void onPostExecute(Integer integer) {
switch (integer){
case TYPE_SUCCESS:
listener.onSuccess();
break;
case TYPE_CANCELED:
listener.onCanceled();
break;
case TYPE_PAUSED:
listener.onPause();
break;
case TYPE_FAILED:
listener.onFailed();
break;
default:
break;
}
}
public void pauseDownload(){
isPaused = true;
}
public void cancelDownload(){
isCanceled = true;
}
private long getContentLength(String downloadUrl) throws IOException{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(downloadUrl)
.build();
Response response = client.newCall(request).execute();
if(response != null && response.isSuccessful()){
long contentLength = response.body().contentLength();
response.close();
Log.i(TAG , "response is not null");
return contentLength;
}else{
Log.i(TAG , "response is null");
}
return 0;
}
}
AsyncTask三个泛型参数,第一个String类型参数表示在执行AsyncTask的时候需要传入一个字符串参数给后台任务,第二个int类型参数表示用整数来作为进度显示单位,第三个int类型参数用来反馈执行结果
package com.slience.downloaddemo.downloadservice;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import com.slience.downloaddemo.MainActivity;
import com.slience.downloaddemo.R;
import com.slience.downloaddemo.downloadinterface.DownloadListener;
import com.slience.downloaddemo.downloadtask.DownloadTask;
import java.io.File;
public class DownloadService extends Service {
private DownloadTask downloadTask;
private String downloadUrl;
private DownloadListener listener = new DownloadListener() {
@Override
public void onProgress(int progress) {
getNotificationManager().notify(1,getNotification("Downloading..." , progress));
}
@Override
public void onSuccess() {
downloadTask = null;
stopForeground(true);
getNotificationManager().notify(1,getNotification("Download Success" , -1));
Toast.makeText(DownloadService.this , "Download Success" , Toast.LENGTH_SHORT).show();
}
@Override
public void onFailed() {
downloadTask = null;
//下载失败将前台服务关闭,并创建一个下载失败的通知
stopForeground(true);
getNotificationManager().notify(1,getNotification("Download Failed" , -1));
Toast.makeText(DownloadService.this , "Download Failed" , Toast.LENGTH_SHORT).show();
}
@Override
public void onPause() {
downloadTask = null;
Toast.makeText(DownloadService.this , "Paused" , Toast.LENGTH_SHORT).show();
}
@Override
public void onCanceled() {
downloadTask = null;
stopForeground(true);
Toast.makeText(DownloadService.this , "Canceled" , Toast.LENGTH_SHORT).show();
}
};
public DownloadService() {
}
private DownloadBinder mBinder = new DownloadBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class DownloadBinder extends Binder{
public void startDownload(String url){
if(downloadTask == null){
downloadUrl = url;
downloadTask = new DownloadTask(listener);
downloadTask.execute(downloadUrl);
startForeground(1 , getNotification("Downloading..." , 0));
Toast.makeText(DownloadService.this , "Downloading" , Toast.LENGTH_SHORT).show();
}
}
public void pauseDownload(){
if(downloadTask != null){
downloadTask.pauseDownload();
}
}
public void cancelDownload(){
if(downloadTask != null){
downloadTask.cancelDownload();
}else{
if(downloadUrl != null){
String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
File file = new File(directory + fileName);
if(file.exists()){
file.delete();
}
getNotificationManager().cancel(1);
stopForeground(true);
Toast.makeText(DownloadService.this , "Canceled" , Toast.LENGTH_SHORT).show();
}
}
}
}
private NotificationManager getNotificationManager(){
return (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
private Notification getNotification(String title , int progress){
Intent intent = new Intent(this , MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this , 0 , intent , 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources() , R.mipmap.ic_launcher));
builder.setContentIntent(pi);
builder.setContentTitle(title);
if(progress > 0){
builder.setContentText(progress + "%");
builder.setProgress(100 , progress , false);
}
return builder.build();
}
}
getNotificationManager()方法用来调用NotificationManager的notify()方法,触发通知不停的更新当前的下载进度
DownloadBinder中提供了startDownload(),pauseDownload(),cancelDownload()三个方法,分别用于开始下载,暂停下载,和取消下载
package com.slience.downloaddemo;
import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.slience.downloaddemo.downloadservice.DownloadService;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private DownloadService.DownloadBinder downloadBinder;
private Button startDwonload , pauseDownload , cancelDownload;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder = (DownloadService.DownloadBinder)service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startDwonload = (Button)findViewById(R.id.start_download);
pauseDownload = (Button)findViewById(R.id.pause_download);
cancelDownload = (Button)findViewById(R.id.cancel_download);
startDwonload.setOnClickListener(this);
pauseDownload.setOnClickListener(this);
cancelDownload.setOnClickListener(this);
Intent intent = new Intent(this , DownloadService.class);
startService(intent);
bindService(intent , connection , BIND_AUTO_CREATE); //绑定服务
if(ContextCompat.checkSelfPermission(MainActivity.this , Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this , new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE} , 1);
}
}
@Override
public void onClick(View v) {
if(downloadBinder == null){
return;
}
switch (v.getId()){
case R.id.start_download:
String url = "http://imtt.dd.qq.com/16891/FDC777ED452DD64653166EDFBF9273D4.apk";
downloadBinder.startDownload(url);
break;
case R.id.pause_download:
downloadBinder.pauseDownload();
break;
case R.id.cancel_download:
downloadBinder.cancelDownload();
break;
default:
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if(grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this , "拒绝权限将无法使用程序" , Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
由于程序使用了网络和调用SD卡的功能,需要在Manifest中添加INTERNET和WRITE_EXTERNAL_STORAGE这两个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">uses-permission>
<uses-permission android:name="android.permission.INTERNET">uses-permission>