簡體   English   中英

帶有此代碼的ProgressBar?

[英]ProgressBar with this code?

我正在此代碼中下載文件,是否可能有一個進度條顯示下載進度? 我嘗試使用異步下載文件並有進度條,但每次都失敗。 進度條位於下載時顯示的對話框中。

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;
    }

}

修復:)(在朋友的幫助下)

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM