android java下载文件_Android文件下载详解(仿第一行代码)

package com.example.dowmloadfile.Myservice;

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.example.dowmloadfile.MainActivity;

import com.example.dowmloadfile.Myclass.DownLoadTask;

import com.example.dowmloadfile.R;

import com.example.dowmloadfile.imp.DownloadListener;

import java.io.File;public classDownloadService extends Service {privateDownLoadTask downLoadTask;privateString downloadUrl;//创建一个DownloadListener并实现其中的方法

private DownloadListener downloadListener=newDownloadListener() {

@Override//以通知的方式显示进度条

public void onProgress(intprogress) {//使用getNotificationManager函数构建一个用于显示下载进度的通知//使用notify去触发这个通知

getNotificationManager().notify(1,getNotification("Download...",progress));

}

@Overridepublic voidonSuccess() {

downLoadTask=null;//关闭前台服务通知//下面自己写了一个通知用于通知下载成功

stopForeground(true);

getNotificationManager().notify(1,getNotification("download succeed",-1));

Toast.makeText(DownloadService.this,"Download Succeed",Toast.LENGTH_LONG).show();

String directory=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();

}

@Overridepublic voidonFiled() {

downLoadTask=null;//关闭前台服务通知

stopForeground(true);

getNotificationManager().notify(1,getNotification("download filed",-1));

Toast.makeText(DownloadService.this,"Download failed",Toast.LENGTH_LONG).show();

}

@Overridepublic voidonPaused() {

downLoadTask=null;

Toast.makeText(DownloadService.this,"Download Paused",Toast.LENGTH_LONG).show();

}

@Overridepublic voidinCanceled() {

downLoadTask=null;//关闭前台服务通知

stopForeground(true);

Toast.makeText(DownloadService.this,"Download canceled",Toast.LENGTH_LONG).show();

}

};//用于使服务可以和活动通信

private DownloadBinder mBinder=newDownloadBinder();

@OverridepublicIBinder onBind(Intent intent) {//TODO: Return the communication channel to the service.

returnmBinder;

}//用于使服务可以和活动通信

public classDownloadBinder extends Binder{//开始下载

public voidstartDownload(String url){if(downLoadTask==null){

downloadUrl=url;//创建一个DownLoadTask,将上面的downloadListener传入

downLoadTask=newDownLoadTask(downloadListener);//使用execute开启下载

downLoadTask.execute(downloadUrl);//startForeground使服务成为一个前台服务以创建持续运行的通知

startForeground(1,getNotification("download...",0));

Toast.makeText(DownloadService.this,"Download",Toast.LENGTH_LONG).show();

}

}//暂停下载

public voidpauseDownload(){if (downLoadTask!=null){

downLoadTask.pauseDownload();

}

}//取消下载后需要将下载中的任务取消

public voidcancelDownload(){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_LONG).show();

}

}

}

}privateNotificationManager getNotificationManager(){return(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

}private Notification getNotification(String title,intprogress){

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);//最大进度,当前进度,是否使用模糊进度条

}returnbuilder.build();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值