帮忙看下这段代码,我的想法是加一个常量,然后后面下载时做一个控制每次下载的个数就是这个常量。比如说:常量值为1,就是每次下载的数量为1.
还有一个问题,就是下载的时候播放音乐,音乐会有点卡这是什么原因。
package com.teleca.jamendo.activity;import java.util.List;import com.teleca.jamendo.R;
import com.teleca.jamendo.util.Downloader;
import com.teleca.jamendo.util.Info;
import com.teleca.jamendo.util.InfoDao;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;public class downloadActivity extends Activity { 
 private LinearLayout rootLinearLayout;  
 private static InfoDao dao = null ; 
 private String[] urls;
 private String[] titles;
 private String[] actors;
 private String[] playtimes; 
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.downloadview); 
         Intent intent = new Intent();
     intent.setAction("com.alex.stopdownload");
     sendBroadcast(intent);
        
        if(dao==null){
         dao = new InfoDao(this);
        }         
         rootLinearLayout = (LinearLayout) findViewById(R.id.root);   
   
         // 窗体创建之后, 查询数据库是否有未完成任务, 如果有, 创建进度条等组件, 继续下载   
         List<Info> list = dao.queryUndone();  
         for (Info info : list){  
             createDownload(info);
         } 
         
         Intent extintent = this.getIntent();
 Bundle bundle = extintent.getExtras();  
         if(bundle!=null){
          urls = bundle.getStringArray("urls");
          titles = bundle.getStringArray("title");
          playtimes = bundle.getStringArray("playtime");
          actors = bundle.getStringArray("actorsname");
                 if(urls!=null&&titles!=null&&playtimes!=null&&actors!=null){
                     for(int i=0;i<urls.length;i++){
                      boolean ishas = false;
                      for(int j=0;j<list.size();j++){
                      if(titles[i].equals(list.get(j).getTitle())){
                     ishas = true; 
                      }
                      }
                      if(ishas){
                      continue;
                      } 
                      Info downloadinfo = new Info(urls[i], titles[i], actors[i], playtimes[i], i, 0);
                      createDownload(downloadinfo);
                     }
                 }           
         }                    
     }       
   
     /** 
      * 动态生成新View 
      * 初始化表单数据 
      * @param path 
      */  
     private void createDownload(Info info) {  
         //获取系统服务LayoutInflater,用来生成新的View   
         LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
         LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.download, null);              
         ProgressBar progressBar = (ProgressBar) linearLayout.findViewById(R.id.downloadpb);
         TextView textView = (TextView) linearLayout.findViewById(R.id.downloadtext);
         TextView dltitle = (TextView) linearLayout.findViewById(R.id.dltitle);
         TextView dlactor = (TextView) linearLayout.findViewById(R.id.dlactor);
         TextView dlplaytime = (TextView) linearLayout.findViewById(R.id.dlplaytime);
         
         ImageView button = (ImageView) linearLayout.findViewById(R.id.startorpause);
   
         dltitle.setText(info.getTitle());
         dlactor.setText(info.getActor());
         dlplaytime.setText(info.getPlaytime());
         try {  
             button.setOnClickListener(new MyListener(progressBar, textView, info));  
             button.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
((View)v.getParent()).setBackgroundResource(R.drawable.mymusickuicon_01);
}else{
((View)v.getParent()).setBackgroundResource(R.drawable.bar_hover);
}

}
});
             //调用当前页面中某个容器的addView,将新创建的View添加进来   
             rootLinearLayout.addView(linearLayout);  
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
     }  
   
     private final class MyListener implements OnClickListener {  
         private ProgressBar progressBar;  
         private TextView textView;  
         private int fileLen;  
         private Downloader downloader;  
         private String name;  
         private boolean ispause=false;
           
         /** 
          * 执行下载 
          * @param progressBar //进度条 
          * @param textView //百分比 
          * @param path  //下载文件路径 
          */  
         public MyListener(ProgressBar progressBar, TextView textView, Info info) {  
             this.progressBar = progressBar;  
             this.textView = textView;  
             name = info.getTitle();  
   
             downloader = new Downloader(getApplicationContext(), handler,dao);  
             try {  
                 downloader.download(info, 1);  
             } catch (Exception e) {  
                 e.printStackTrace();  
                 Toast.makeText(getApplicationContext(), "下载过程中出现异常", 0).show();  
                 throw new RuntimeException(e);  
             }  
         }  
           
         //Handler传输数据   
         private Handler handler = new Handler() {  
             @Override  
             public void handleMessage(Message msg) {  
                 switch (msg.what) {  
                     case 0:  
                         //获取文件的大小   
                         fileLen = msg.getData().getInt("fileLen");  
                         //设置进度条最大刻度:setMax()   
                         progressBar.setMax(fileLen);  
                        break;  
                     case 1:  
                        //获取当前下载的总量   
                        int done = msg.getData().getInt("done");  
                         //当前进度的百分比   
                         textView.setText((float)(Math.round((((float)done)/(1024*1024))*10))/10 + "mb/"+(float)(Math.round((((float)fileLen)/(1024*1024))*10))/10+"mb-"  +((long)done)*100 / fileLen+"%");  
                         //进度条设置当前进度:setProgress()   
                         progressBar.setProgress(done);  
                        if (done == fileLen) {  
                             Toast.makeText(getApplicationContext(), name + " 下载完成", 0).show();  
                             //下载完成后退出进度条   
                             rootLinearLayout.removeView((View) progressBar.getParent());  
                         }  
                         break;  
                 }  
             }  
         };  
   
         /** 
          * 暂停和继续下载 
          */  
         public void onClick(View v) {  
          ImageView pauseButton = (ImageView) v;  
             if (!ispause) {  
                 downloader.pause();  
                 pauseButton.setImageResource(R.drawable.startdown); 
                 ispause=true;
             } else {  
                 downloader.resume();  
                 pauseButton.setImageResource(R.drawable.pausedownload); 
                 ispause=false;
             }  
         }  
     } @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}      
}