在网上真心没找到实现这样的代码,工作需要,只能苦X 自己实现了。
package com.example.services;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
import com.example.bean.DownloadTask;
import com.example.bean.NotificationBean;
import com.example.notification06.R;
import com.example.utils.DownloadUtil;
import com.example.utils.DownloadUtil.IOnDownloadListener;
public class DownloadServices extends Service
{
private Context mContext = DownloadServices.this;
/** 正在下载 */
private final int DOWN_LOADING = 0;
/** 下载完成 */
private final int DOWN_COMPLETE = 1;
/** 下载失败 */
private final int DOWN_ERR = 2;
/** Timer 执行时间间隔 */
private final int TIMER_PERIOD = 1500;
protected Timer mTimer;
protected NotificationManager mNotificationManager;
/** 下载任务管理 */
protected Map<String, DownloadTask> map_downloadtask;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
mTimer = new Timer();
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
map_downloadtask = new HashMap<String, DownloadTask>();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
String mUrl = intent.getExtras().getString("url");
int mNotifyId = intent.getExtras().getInt("notifyId");
Notification mNotification = new NotificationBean(this, R.drawable.ic_launcher, "开始下载", System.currentTimeMillis());
System.out.println("NotifyId = " mNotifyId);
if(map_downloadtask.containsKey(mUrl)){
Toast.makeText(mContext, "已存在此下载任务", Toast.LENGTH_SHORT).show();
}
else{
DownloadTask mDownloadTask = new DownloadTask();
mDownloadTask.setUrl(mUrl);
mDownloadTask.setNotifyID(mNotifyId);
mDownloadTask.setNotification(mNotification);
map_downloadtask.put(mUrl, mDownloadTask);
Runnable mRunnable = new MyRunnable(mDownloadTask);
new Thread(mRunnable).start();
}
return super.onStartCommand(intent, flags, startId);
}
class MyRunnable implements Runnable
{
private DownloadUtil mDownUtil = new DownloadUtil();
private DownloadTask mDownTask;
private Handler mHandler;
private TimerTask mTimerTask;
public MyRunnable(DownloadTask downTask)
{
super();
this.mDownTask = downTask;
this.mHandler = new MyHandler(mDownUtil);
this.mTimerTask = new MyTimerTask(mDownUtil, mHandler, mDownTask);
}
@Override
public void run()
{
mTimer.schedule(mTimerTask, 0, TIMER_PERIOD);
mDownUtil.downloadFile(mDownTask.getUrl());
}
}
class MyTimerTask extends TimerTask
{
private Handler mHandler;
private DownloadUtil mDownUtil;
private DownloadTask mDownTask;
private IOnDownloadListener mListener;
public MyTimerTask(DownloadUtil downUtil, Handler handler, DownloadTask downTask)
{
super();
this.mHandler = handler;
this.mDownUtil = downUtil;
this.mDownTask = downTask;
this.mListener = new IOnDownloadListener()
{
@Override
public void updateNotification(int progress, int totalSize, File downFile)
{
// TODO Auto-generated method stub
int rate = 0;
// 计算百分比
if (totalSize > 0)
{
rate = progress * 100 / totalSize;
mHandler.obtainMessage(DOWN_LOADING, rate, mDownTask.getNotifyID(), mDownTask.getNotification()).sendToTarget();
}
else if (totalSize == 0)
{
mHandler.obtainMessage(DOWN_LOADING, 0, mDownTask.getNotifyID(), mDownTask.getNotification()).sendToTarget();
}
else {
cancel();
mHandler.obtainMessage(DOWN_ERR, mDownTask).sendToTarget();
}
// 是否下载结束
if (totalSize > 0 && null != downFile && totalSize == (int) downFile.length())
{
cancel();
mHandler.obtainMessage(DOWN_COMPLETE, downFile).sendToTarget();
map_downloadtask.remove(mDownTask.getUrl());// 移除已完成任务
System.out.println("DOWN_COMPLETE ==> totalSize ==> " totalSize);
}
}
};
}
@Override
public void run()
{
mDownUtil.setOnDownloadListener(mListener);
}
}
class MyHandler extends Handler
{
private DownloadUtil mDownUtil;
public MyHandler(DownloadUtil downUtil)
{
super();
this.mDownUtil = downUtil;
}
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case DOWN_LOADING:
((Notification)msg.obj).contentView.setProgressBar(R.id.pb, 100, msg.arg1, false);
((Notification)msg.obj).contentView.setTextViewText(R.id.tv, "下载" msg.arg1 "%");
mNotificationManager.notify(msg.arg2, ((Notification)msg.obj));
System.out.println("DOWN_LOADING --> mNotifyId --> " msg.arg2 " --> " msg.arg1 "%");
break;
case DOWN_COMPLETE:
// mNotificationManager.cancel(mNotifyId);
removeMessages(DOWN_LOADING);
Toast.makeText(mContext, "下载完成", Toast.LENGTH_SHORT).show();
mDownUtil.installApk(mContext, (File)msg.obj);
System.out.println("======================DOWN_COMPLETE================================");
stopService();
break;
case DOWN_ERR:
removeMessages(DOWN_LOADING);
map_downloadtask.remove(((DownloadTask)msg.obj).getUrl());
Toast.makeText(mContext, "下载失败", Toast.LENGTH_SHORT).show();
stopService();
break;
default:
break;
}
}
/**
* 如果无下载任务,关闭服务
*/
private void stopService(){
if(map_downloadtask.isEmpty()){
stopSelf(-1);
}
}
}
}
资源文件列表
DemoNotification_06/.classpath , 443
DemoNotification_06/.project , 855
DemoNotification_06/.settings/org.eclipse.jdt.core.prefs , 598
DemoNotification_06/AndroidManifest.xml , 1254
DemoNotification_06/bin/AndroidManifest.xml , 1254
DemoNotification_06/bin/classes/com/example/bean/DownloadTask.class , 1055
DemoNotification_06/bin/classes/com/example/bean/NotificationBean.class , 1395
DemoNotification_06/bin/classes/com/example/notification06/BuildConfig.class , 361
DemoNotification_06/bin/classes/com/example/notification06/NotificationActivity06.class , 1875
DemoNotification_06/bin/classes/com/example/notification06/R$attr.class , 364
DemoNotification_06/bin/classes/com/example/notification06/R$dimen.class , 485
DemoNotification_06/bin/classes/com/example/notification06/R$drawable.class , 431
DemoNotification_06/bin/classes/com/example/notification06/R$id.class , 582
DemoNotification_06/bin/classes/com/example/notification06/R$layout.class , 462
DemoNotification_06/bin/classes/com/example/notification06/R$menu.class , 412
DemoNotification_06/bin/classes/com/example/notification06/R$string.class , 557
DemoNotification_06/bin/classes/com/example/notification06/R$style.class , 455
DemoNotification_06/bin/classes/com/example/notification06/R.class , 753
DemoNotification_06/bin/classes/com/example/services/DownloadServices$MyHandler.class , 2910
DemoNotification_06/bin/classes/com/example/services/DownloadServices$MyRunnable.class , 1715
DemoNotification_06/bin/classes/com/example/services/DownloadServices$MyTimerTask$1.class , 2593
DemoNotification_06/bin/classes/com/example/services/DownloadServices$MyTimerTask.class , 1880
DemoNotification_06/bin/classes/com/example/services/DownloadServices.class , 3828
DemoNotification_06/bin/classes/com/example/utils/DownloadUtil$IOnDownloadListener.class , 283
DemoNotification_06/bin/classes/com/example/utils/DownloadUtil.class , 3887
DemoNotification_06/bin/classes.dex , 17424
DemoNotification_06/bin/DemoNotification_06.apk , 52022
DemoNotification_06/bin/dexedLibs/annotations-6b0373606d8e6b5649a18d5335b2cee9.jar , 943
DemoNotification_06/bin/dexedLibs/annotations-e80ee1b2e069a35b6b9fce7468f32699.jar , 943
DemoNotification_06/bin/jarlist.cache , 120
DemoNotification_06/bin/res/drawable-hdpi/ic_launcher.png , 5964
DemoNotification_06/bin/res/drawable-mdpi/ic_launcher.png , 3112
DemoNotification_06/bin/res/drawable-xhdpi/ic_launcher.png , 9355
DemoNotification_06/bin/res/drawable-xxhdpi/ic_launcher.png , 17889
DemoNotification_06/bin/resources.ap_ , 42037
DemoNotification_06/gen/com/example/notification06/BuildConfig.java , 168
DemoNotification_06/gen/com/example/notification06/R.java , 2522
DemoNotification_06/ic_launcher-web.png , 51394
DemoNotification_06/libs/android-support-v4.jar , 393154
DemoNotification_06/proguard-project.txt , 781
DemoNotification_06/project.properties , 562
DemoNotification_06/res/drawable-hdpi/ic_launcher.png , 7658
DemoNotification_06/res/drawable-mdpi/ic_launcher.png , 3777
DemoNotification_06/res/drawable-xhdpi/ic_launcher.png , 12516
DemoNotification_06/res/drawable-xxhdpi/ic_launcher.png , 24777
DemoNotification_06/res/layout/activity_main.xml , 1213
DemoNotification_06/res/layout/remote_view.xml , 1153
DemoNotification_06/res/menu/main.xml , 270
DemoNotification_06/res/values/dimens.xml , 218
DemoNotification_06/res/values/strings.xml , 352
DemoNotification_06/res/values/styles.xml , 695
DemoNotification_06/res/values-sw600dp/dimens.xml , 201
DemoNotification_06/res/values-sw720dp-land/dimens.xml , 275
DemoNotification_06/src/com/example/bean/DownloadTask.java , 740
DemoNotification_06/src/com/example/bean/NotificationBean.java , 1084
DemoNotification_06/src/com/example/notification06/NotificationActivity06.java , 1207
DemoNotification_06/src/com/example/services/DownloadServices.java , 6222
DemoNotification_06/src/com/example/utils/DownloadUtil.java , 3184
