原文网址:http://www.obatu.com/android-service-download-notification-progressbar/

 

 最近的项目中,有一个需求是 【后台下载+多线程+Notificaton进度条】的应用。要求有一个Service做下载服务,Notification中显示一进度条,进度条由Service来更新。
花了个把小时在网上找代码,没有一个完美的方案,最后根据片段信息,自己实现了。贴出来分享一下。

思路:

  • 1. 有一个提供队列下载的Service
  • 2. 该Service可以绑定,绑定的时候返回本身实例
  • 3. 该Service提供方法给绑定了该Service的Activity,Activity可以调用Service的提供的方法,给Service添加队列
  • 4. 该Service提供了当前下载队列的信息,信息包括下载的名称,下载进度,Activity通过接口读取队列信息用于操作,如:取消


实现:

1. 队列类

  1. public class DownLoadQueue{
  2.  
  3.     public int id; //队列id , 该id标识了一条下载线程和Notification
  4.  
  5.     public String name; // 队列名称,用于显示在Notification中
  6.  
  7.      public int progress = 0; //进度,一个0-100之前的整数,标识百分比
  8.  
  9.      public boolean isCancel = fale; //是否已经取消,该开关用于取消下载
  10.  

2. Service类:

   
  1.   public class DownloadService extends Service{
  2.  
  3.            private Map downLoadQueue; //下载队列,格式:
  4.  
  5.              private Binder serviceBinder = new DownLoadServiceBinder();
  6.  
  7.                // Activity绑定后,会自动条用这个方法
  8.                @Override
  9.                public IBinder onBind(Intent intent)
  10.                {
  11.                     Log.d("DownloadService", "onBind");
  12.                     return serviceBinder;
  13.                }
  14.  
  15.                ....
  16.  
  17.                private void startDownLoad(DownLoadQueue queue){
  18.                     new Thread(){
  19.                          public void run(){
  20.                               //进行下载工作,这里需要更新downLoadQueue中对应的queue的进度信息
  21.                               //使用Handler更新notification信息
  22.                               ....
  23.                          }
  24.  
  25.                     }.start();
  26.                }
  27.  
  28.                public void addQueue(DownLoadQueue queue){
  29.                     this.downLoadQueue.add(queue.id,queue);
  30.                     this.startDownLoad(queue);
  31.                }
  32.  
  33.                public Map getQueueList(){
  34.                     return this.downLoadQueue;
  35.                }
  36.  
  37.              public class public DownLoadServiceBinder extends Binder{
  38.                   public DownloadService getService(){
  39.                          return DownloadService.this;
  40.                     }
  41.              },downloadqueue>,队列>,downloadqueue>

3. Activity类:

   
  1. public class DownLoadActivity{
  2.           private Handler handler = new Handler();
  3.           private DownloadService downLoadService;
  4.           private ServiceConnection serviceConnection = new ServiceConnection()
  5.           {
  6.                // 连接服务失败后,该方法被调用
  7.                @Override
  8.                public void onServiceDisconnected(ComponentName name)
  9.                {
  10.                     downLoadService = null;
  11.                     Toast.makeText(DownLoadActivity.this, "Service Failed.", Toast.LENGTH_LONG).show();
  12.                }
  13.                // 成功连接服务后,该方法被调用。在该方法中可以获得downLoadService对象
  14.                @Override
  15.                public void onServiceConnected(ComponentName name, IBinder service)
  16.                {
  17.                     // 获得downLoadService对象
  18.                     downLoadService = ((DownLoadService.DownLoadServiceBinder) service).getService();
  19.                     Toast.makeText(DownLoadActivity.this, "Service Connected.", Toast.LENGTH_LONG).show();
  20.                }
  21.           };
  22.  
  23.           @Override
  24.           public void onCreate(Bundle savedInstanceState){
  25.                super.onCreate(savedInstanceState);
  26.                Intent serviceIntent = new Intent(DownLoadActivity.this,DownloadService.class);
  27.                bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
  28.                startService(serviceIntent);
  29.                .....
  30.           }
  31.  
  32.           private void startDownLoad(){
  33.                DownLoadQueue queue = new DownLoadQueue();
  34.                queue.id = 1;
  35.                queue.name = "愤怒的小鸟"
  36.                ...
  37.                downLoadService.addDownLoad(queue);
  38.                ....
  39.           }
  40.  
  41.           @Override
  42.           public void onDestroy(){
  43.                super.onDestroy();
  44.                unbindService(new Intent(DownLoadActivity.this,DownloadService.class));
  45.           }

4. 在Service中更新Notification时,不能过于频繁,否则会造成卡机。notification使用RemoteView来自定义Notification,加入进度条加入各种说明文字,等等。

以上文字只是简单的提及了一些思路。在项目中已经实现。下次修改完善后贴上来分享。希望能够将该功能做成一个独立的Service供大家在项目中嵌入使用。

更多相关文章

  1. GLSurfaceView 基本使用与源码解析
  2. android 自动化测试之MonkeyRunner学习(二)
  3. 简单的音频播放功能MediaPlayer使用中所遇到的坑。
  4. Android(安卓)判断当前线程是否是主线程的两种方法
  5. 我的Android进阶之旅------>Android中高低API版本兼容使用@Targe
  6. ContentProvider的工作过程
  7. SystemServer(一):恢复出厂设置后wifi、数据流量、gps设置关闭
  8. Android(安卓)ListView列表视图的使用方法
  9. 记录的Android开发过程中遇到的问题。

随机推荐

  1. 表单与css
  2. 念念不忘,必有回响!6月更文活动的一些总结
  3. css选择器与用户表单制作
  4. 简单的注册和模块+CSS选择器
  5. 浅谈对http协议的理解
  6. 案例表格的应用
  7. 实战简单的注册表单以及选择器的认识
  8. 前端作业-CSS1
  9. 表单以及选择器
  10. 注册表单、基本选择器及上下文选择器