Android多任务下载,使用Notification显示、更新进度条

来源:互联网 发布:alt 左键 ubuntu 编辑:程序博客网 时间:2024/05/18 03:37

使用Notification显示、更新多任务下载进度。

在网上真心没找到实现这样的代码,工作需要,只能苦X 自己实现了。

由于水平有限,只是实现了功能,代码功底神马的,就不要挑剔了哈,大牛请绕道!!!

 

如图:


 

启动页面NotificationActivity06:(从这文件名,就可以看出我苦X 了多少个版本)

Java代码  收藏代码
  1. package com.example.notification06;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8.   
  9. import com.example.services.DownloadServices;  
  10.   
  11. public class NotificationActivity06 extends Activity  
  12. {  
  13.     private Context mContext = NotificationActivity06.this;  
  14.       
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState)  
  17.     {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.     }  
  21.       
  22.     public void downloadNP(View view)  
  23.     {  
  24.         startDownloadService(0"http://61.50.254.57:8088/nature-person/mobilenature/download/NaturalSaler_common.apk");  
  25.     }  
  26.       
  27.     public void downloadYUN(View view)  
  28.     {  
  29.         startDownloadService(1"http://mix.911860.com/resources/apkupdate/7/common/mix-common.apk");  
  30.     }  
  31.       
  32.     public void downloadTNews(View view)  
  33.     {  
  34.         startDownloadService(2"http://mix.911860.com/resources/mix/custom-apks/01-TencentNews/TencentNews.apk");  
  35.     }  
  36.       
  37.     public void startDownloadService(int notifyId, String url)  
  38.     {  
  39.         Intent i = new Intent(mContext, DownloadServices.class);  
  40.         i.putExtra("url", url);  
  41.         i.putExtra("notifyId", notifyId);  
  42.         mContext.startService(i);  
  43.     }  
  44.   
  45. }  

 其中的下载链接,换成自己需要的就好了。

 

下载服务DownloadServices:

Java代码  收藏代码
  1. package com.example.services;  
  2.   
  3. import java.io.File;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6. import java.util.Timer;  
  7. import java.util.TimerTask;  
  8.   
  9. import android.app.Notification;  
  10. import android.app.NotificationManager;  
  11. import android.app.Service;  
  12. import android.content.Context;  
  13. import android.content.Intent;  
  14. import android.os.Handler;  
  15. import android.os.IBinder;  
  16. import android.os.Message;  
  17. import android.widget.Toast;  
  18.   
  19. import com.example.bean.DownloadTask;  
  20. import com.example.bean.NotificationBean;  
  21. import com.example.notification06.R;  
  22. import com.example.utils.DownloadUtil;  
  23. import com.example.utils.DownloadUtil.IOnDownloadListener;  
  24.   
  25. public class DownloadServices extends Service  
  26. {  
  27.     private Context mContext = DownloadServices.this;  
  28.     /** 正在下载 */  
  29.     private final int DOWN_LOADING = 0;  
  30.     /** 下载完成 */  
  31.     private final int DOWN_COMPLETE = 1;  
  32.     /** 下载失败 */  
  33.     private final int DOWN_ERR = 2;  
  34.     /** Timer 执行时间间隔 */  
  35.     private final int TIMER_PERIOD = 1500;  
  36.   
  37.     protected Timer mTimer;  
  38.     protected NotificationManager mNotificationManager;  
  39.     /** 下载任务管理 */  
  40.     protected Map<String, DownloadTask> map_downloadtask;  
  41.   
  42.     @Override  
  43.     public IBinder onBind(Intent intent)  
  44.     {  
  45.         return null;  
  46.     }  
  47.   
  48.     @Override  
  49.     public void onDestroy()  
  50.     {  
  51.         super.onDestroy();  
  52.     }  
  53.   
  54.     @Override  
  55.     public void onCreate()  
  56.     {  
  57.         // TODO Auto-generated method stub  
  58.         super.onCreate();  
  59.         mTimer = new Timer();  
  60.         mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  61.         map_downloadtask = new HashMap<String, DownloadTask>();  
  62.     }  
  63.   
  64.       
  65.     @Override  
  66.     public int onStartCommand(Intent intent, int flags, int startId)  
  67.     {  
  68.         String mUrl = intent.getExtras().getString("url");  
  69.         int mNotifyId = intent.getExtras().getInt("notifyId");  
  70.         Notification mNotification = new NotificationBean(this, R.drawable.ic_launcher, "开始下载", System.currentTimeMillis());  
  71.         System.out.println("NotifyId = " + mNotifyId);  
  72.           
  73.         if(map_downloadtask.containsKey(mUrl)){  
  74.             Toast.makeText(mContext, "已存在此下载任务", Toast.LENGTH_SHORT).show();  
  75.         }  
  76.         else{  
  77.             DownloadTask mDownloadTask = new DownloadTask();  
  78.             mDownloadTask.setUrl(mUrl);  
  79.             mDownloadTask.setNotifyID(mNotifyId);  
  80.             mDownloadTask.setNotification(mNotification);  
  81.             map_downloadtask.put(mUrl, mDownloadTask);  
  82.               
  83.             Runnable mRunnable = new MyRunnable(mDownloadTask);  
  84.             new Thread(mRunnable).start();  
  85.         }  
  86.   
  87.         return super.onStartCommand(intent, flags, startId);  
  88.     }  
  89.   
  90.   
  91.       
  92.     class MyRunnable implements Runnable  
  93.     {  
  94.         private DownloadUtil mDownUtil = new DownloadUtil();  
  95.         private DownloadTask mDownTask;  
  96.         private Handler mHandler;  
  97.         private TimerTask mTimerTask;  
  98.   
  99.         public MyRunnable(DownloadTask downTask)  
  100.         {  
  101.             super();  
  102.             this.mDownTask = downTask;  
  103.             this.mHandler = new MyHandler(mDownUtil);  
  104.             this.mTimerTask = new MyTimerTask(mDownUtil, mHandler, mDownTask);  
  105.         }  
  106.   
  107.         @Override  
  108.         public void run()  
  109.         {  
  110.             mTimer.schedule(mTimerTask, 0, TIMER_PERIOD);  
  111.             mDownUtil.downloadFile(mDownTask.getUrl());  
  112.         }  
  113.   
  114.     }  
  115.   
  116.       
  117.     class MyTimerTask extends TimerTask  
  118.     {  
  119.         private Handler mHandler;  
  120.         private DownloadUtil mDownUtil;  
  121.         private DownloadTask mDownTask;  
  122.         private IOnDownloadListener mListener;  
  123.           
  124.         public MyTimerTask(DownloadUtil downUtil, Handler handler, DownloadTask downTask)  
  125.         {  
  126.             super();  
  127.             this.mHandler = handler;  
  128.             this.mDownUtil = downUtil;  
  129.             this.mDownTask = downTask;  
  130.             this.mListener = new IOnDownloadListener()  
  131.             {  
  132.                 @Override  
  133.                 public void updateNotification(int progress, int totalSize, File downFile)  
  134.                 {  
  135.                     // TODO Auto-generated method stub  
  136.                     int rate = 0;  
  137.                     // 计算百分比  
  138.                     if (totalSize > 0)  
  139.                     {  
  140.                         rate = progress * 100 / totalSize;  
  141.                         mHandler.obtainMessage(DOWN_LOADING, rate, mDownTask.getNotifyID(), mDownTask.getNotification()).sendToTarget();  
  142.                     }  
  143.                     else if (totalSize == 0)  
  144.                     {  
  145.                         mHandler.obtainMessage(DOWN_LOADING, 0, mDownTask.getNotifyID(), mDownTask.getNotification()).sendToTarget();  
  146.                     }  
  147.                     else {  
  148.                         cancel();  
  149.                         mHandler.obtainMessage(DOWN_ERR, mDownTask).sendToTarget();  
  150.                     }  
  151.                     // 是否下载结束  
  152.                     if (totalSize > 0 && null != downFile && totalSize == (int) downFile.length())  
  153.                     {  
  154.                         cancel();  
  155.                         mHandler.obtainMessage(DOWN_COMPLETE, downFile).sendToTarget();  
  156.                         map_downloadtask.remove(mDownTask.getUrl());// 移除已完成任务  
  157.                         System.out.println("DOWN_COMPLETE ==> totalSize ==> " + totalSize);  
  158.                     }  
  159.                 }  
  160.   
  161.             };  
  162.         }  
  163.   
  164.         @Override  
  165.         public void run()  
  166.         {  
  167.             mDownUtil.setOnDownloadListener(mListener);  
  168.         }  
  169.     }  
  170.       
  171.   
  172.     class MyHandler extends Handler  
  173.     {  
  174.         private DownloadUtil mDownUtil;  
  175.   
  176.         public MyHandler(DownloadUtil downUtil)  
  177.         {  
  178.             super();  
  179.             this.mDownUtil = downUtil;  
  180.         }  
  181.   
  182.         @Override  
  183.         public void handleMessage(Message msg)  
  184.         {  
  185.             switch (msg.what)  
  186.             {  
  187.             case DOWN_LOADING:  
  188.                 ((Notification)msg.obj).contentView.setProgressBar(R.id.pb, 100, msg.arg1, false);  
  189.                 ((Notification)msg.obj).contentView.setTextViewText(R.id.tv, "下载" + msg.arg1 + "%");  
  190.                 mNotificationManager.notify(msg.arg2, ((Notification)msg.obj));  
  191.                 System.out.println("DOWN_LOADING --> mNotifyId --> " + msg.arg2 + " --> " + msg.arg1 + "%");  
  192.                 break;  
  193.             case DOWN_COMPLETE:  
  194. //              mNotificationManager.cancel(mNotifyId);  
  195.                 removeMessages(DOWN_LOADING);  
  196.                 Toast.makeText(mContext, "下载完成", Toast.LENGTH_SHORT).show();  
  197.                 mDownUtil.installApk(mContext, (File)msg.obj);  
  198.                 System.out.println("======================DOWN_COMPLETE================================");  
  199.                 stopService();  
  200.                 break;  
  201.             case DOWN_ERR:  
  202.                 removeMessages(DOWN_LOADING);  
  203.                 map_downloadtask.remove(((DownloadTask)msg.obj).getUrl());  
  204.                 Toast.makeText(mContext, "下载失败", Toast.LENGTH_SHORT).show();  
  205.                 stopService();  
  206.                 break;  
  207.             default:  
  208.                 break;  
  209.             }  
  210.         }  
  211.           
  212.         /** 
  213.          * 如果无下载任务,关闭服务 
  214.          */  
  215.         private void stopService(){  
  216.             if(map_downloadtask.isEmpty()){  
  217.                 stopSelf(-1);  
  218.             }  
  219.         }  
  220.   
  221.     }  
  222.   
  223. }  

 

封装Notification:

Java代码  收藏代码
  1. package com.example.bean;  
  2.   
  3. import android.app.Notification;  
  4. import android.app.PendingIntent;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.widget.RemoteViews;  
  8.   
  9. import com.example.notification06.NotificationActivity06;  
  10. import com.example.notification06.R;  
  11.   
  12. public class NotificationBean extends Notification  
  13. {  
  14.     private Context mContext;  
  15.   
  16.     public NotificationBean(Context context, int icon, CharSequence tickerText, long when)  
  17.     {  
  18.         super(icon, tickerText, when);  
  19.         this.mContext = context;  
  20.         this.flags = Notification.FLAG_AUTO_CANCEL; // |=   
  21. //      this.flags = Notification.FLAG_ONGOING_EVENT;  
  22.   
  23.         RemoteViews mRemoteView = new RemoteViews(mContext.getPackageName(), R.layout.remote_view);  
  24.         this.contentView = mRemoteView;  
  25.   
  26.         Intent intent = new Intent(mContext, NotificationActivity06.class); // 点击安装APK 未实现  
  27.         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  28.         PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  29.         this.contentIntent = pIntent;  
  30.     }  
  31.   
  32. }  

 

封装下载任务DownloadTask:

Java代码  收藏代码
  1. package com.example.bean;  
  2.   
  3. import android.app.Notification;  
  4.   
  5. public class DownloadTask  
  6. {  
  7.     private String url;  
  8.     private int notifyID;  
  9.     private Notification notification;  
  10.   
  11.     public DownloadTask()  
  12.     {  
  13.         // TODO Auto-generated constructor stub  
  14.     }  
  15.   
  16.     public Notification getNotification()  
  17.     {  
  18.         return notification;  
  19.     }  
  20.   
  21.     public void setNotification(Notification notification)  
  22.     {  
  23.         this.notification = notification;  
  24.     }  
  25.   
  26.     public int getNotifyID()  
  27.     {  
  28.         return notifyID;  
  29.     }  
  30.   
  31.     public void setNotifyID(int notifyID)  
  32.     {  
  33.         this.notifyID = notifyID;  
  34.     }  
  35.   
  36.     public String getUrl()  
  37.     {  
  38.         return url;  
  39.     }  
  40.   
  41.     public void setUrl(String url)  
  42.     {  
  43.         this.url = url;  
  44.     }  
  45.   
  46. }  

 

完毕。

  • DemoNotification_06.rar (585.6 KB)
  • 描述: 源码
  • 下载次数: 7
原创粉丝点击