我正在下载此代码中的文件,是否可以有一个显示下载进度的进度条?我尝试使用async下载文件并有一个进度条但我每次都失败了。进度条位于下载时显示的对话框中。
package com.maartendekkers.cmapps;
public class CustomAdapter extends BaseAdapter{
String [] result;
String[] desc;
String [] app;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, String[] prgmAppName, String[] prgmDescList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
desc=prgmDescList;
app=prgmAppName;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
public class Holder
{
TextView tv;
TextView tv2;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.single_row, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.tv2=(TextView) rowView.findViewById(R.id.textView2);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.tv2.setText(desc[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
alert.setTitle("Install?");
alert.setMessage("Do you want to install "+result[position]+"?");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@SuppressWarnings("unused")
public void onClick(DialogInterface dialog, int whichButton) {
//some code
/**
* The directory to store the files in
*/
final String STORAGE_PATH = "cm_apps/";
/**
* Download buffer size
*/
final int BUFFER_SIZE = 1024 * 23;
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context,context.getClass()), 0);
final ProgressDialog plswaitdialog = new ProgressDialog(context);
plswaitdialog.setTitle("Please wait");
plswaitdialog.setMessage("Downloading "+result[position]+"...");
plswaitdialog.setProgressStyle(plswaitdialog.STYLE_HORIZONTAL);
plswaitdialog.setProgress(0);
plswaitdialog.show();
Thread downloadThread = new Thread()
{
public void run()
{
try
{
File cnxDir = new File(Environment.getExternalStorageDirectory(), STORAGE_PATH);
if(!cnxDir.exists())
{
cnxDir.mkdir();
}
File file = new File(cnxDir, result[position]);
URL urlObj = new URL("http://www.maartenn.eu/cmapps/"+app[position]+".apk");
URLConnection con = urlObj.openConnection();
BufferedInputStream bis = new BufferedInputStream(con.getInputStream(), BUFFER_SIZE);
FileOutputStream fos = new FileOutputStream(file+".apk");
byte[] bArray = new byte[BUFFER_SIZE];
int current = 0;
int read = 0;
while(current != -1)
{
plswaitdialog.setProgress(result.length);
fos.write(bArray,0,current);
current = bis.read(bArray, 0, BUFFER_SIZE);
read = read + current;
}
fos.close();
bis.close();
plswaitdialog.dismiss();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/cm_apps/"+result[position]+".apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
catch(Exception ioe)
{
Log.d("DownloadHandler.download", "Error: " + ioe.toString(), ioe);
}
}
};
downloadThread.start();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//cancel
}
});
alert.show();
}
});
return rowView;
}
}
答案 0 :(得分:0)
修正了它:)(在朋友的帮助下)
int lof = con.getContentLength();
int progress = (int)(((float)read * 100) / (float)lof);
plswait.setProgress(progress);
pLog.w("Download", progress+"%");
fos.write(bArray,0,current);
current = bis.read(bArray, 0, BUFFER_SIZE);
read = read + current;