hello world

动态改变service内容添加下载信息

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

思路:

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


实现:

1. 队列类

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

2. Service类:

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

3. Activity类:

  1. public classDownLoadActivity{
  2.           private Handlerhandler = new Handler();
  3.           private DownloadServicedownLoadService;
  4.           private ServiceConnectionserviceConnection = new ServiceConnection()
  5.           {
  6.                // 连接服务失败后,该方法被调用
  7.                @Override
  8.                public void onServiceDisconnected(ComponentNamename)
  9.                {
  10.                     downLoadService = null;
  11.                     Toast.makeText(DownLoadActivity.this, "Service Failed.", Toast.LENGTH_LONG).show();
  12.                }
  13.                // 成功连接服务后,该方法被调用。在该方法中可以获得downLoadService对象
  14.                @Override
  15.                public void onServiceConnected(ComponentNamename, IBinderservice)
  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(BundlesavedInstanceState){
  25.                super.onCreate(savedInstanceState);
  26.                IntentserviceIntent = 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.                DownLoadQueuequeue = 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供大家在项目中嵌入使用

posted @ 2012-08-25 16:06  水御双氛  阅读(118)  评论(0编辑  收藏  举报