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();
}
}