آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
مشکل عدم دسترسی خریداران پیشین به برخی آموزش ها برطرف شد
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

چطوری دانلود فایل رو توی پروگرس بار نمایش بدم اینم سورس (حل شد)

underomeda  8 سال پیش  8 سال پیش
0 0

 
private Activity activity;
public ImageLoader imageLoader;
private String l = "Address MyService";

int dlsize = 0, cdl = 0;
public float o;
Thread thread;


public AdapterMenuList(Activity act) {
this.activity = act;
imageLoader = new ImageLoader(act);

}


public int getCount() {
// TODO Auto-generated method stub
return ActivityMenuList.Menu_ID.size();
}


public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}


public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


public static class ViewHolder {

TextView txtText, txtSubText, txtinfodl;
ImageView imgThumb;
ProgressBar progressBar;

}

ViewHolder holder;


public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menu_list_item, null);
holder = new ViewHolder();

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.txtText = (TextView) convertView.findViewById(R.id.txtText);
holder.txtinfodl = (TextView) convertView.findViewById(R.id.txtinfodl);
holder.txtSubText = (TextView) convertView.findViewById(R.id.txtSubText);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
final String Link = l + ActivityMenuList.Menu_image.get(position);
holder.imgThumb.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
holder.txtText.setText("Download Started ...");
new Thread(new Runnable() {

@Override
public void run() {
fdl(Link);

}
}).start();
}

});

holder.txtText.setText(ActivityMenuList.Menu_name.get(position));
holder.txtSubText.setText("" + ActivityMenuList.Menu_price.get(position) + "MB");

return convertView;
}


private void fdl(String link) {

try {

URL url = new URL(link);
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();

urlc.setRequestMethod("GET");
urlc.setDoOutput(true);
urlc.connect();
dlsize = urlc.getContentLength();

File f = new File(Environment.getExternalStorageDirectory(), getname(link));

FileOutputStream fo = new FileOutputStream(f);

InputStream ins = urlc.getInputStream();

byte[] buffer = new byte[1024];

int bl = 0;

while ((bl = ins.read(buffer)) > 0) {

fo.write(buffer, 0, bl);
cdl += bl;

activity.runOnUiThread(new Runnable() {

@Override
public void run() {

o = ((float) cdl / dlsize) * 100;
holder.txtinfodl.setText((cdl / 1024) + " KB / " + (dlsize / 1024) + "KB - " + (int) o + "%"); نمایش حجم فایل :

}
});

}
activity.runOnUiThread(new Runnable() {

@Override
public void run() {
holder.txtText.setText("Download Completed." + "\n Saved to:" + Environment.getExternalStorageDirectory().toString() + "/" + getname(l));

}
});

fo.close();

}
catch (final Exception e) {
activity.runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(AppData.context, e.toString(), 3000).show();
holder.txtText.setText("Download Fail !!");
}
});

}

}


private String getname(String link) {

String[] t = link.split("/");
int c = t.length;
String s = t[c - 1];
s = s.replace("%20", " ");
return s;

}
 برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال 
jbRhmNqb  8 سال پیش
0 0

این لیسنر رو به صورت یک اینترفیس اینطوری تعریف کنید:(در داخل همون کلاسی که وب سرویستون قرار داره)

   public static interface Listener {
        public void onDataReceive(float data);
        public void onFail();
    }
 

حالا توی اون اکتیویتی که دانلود رو صدا میزنین یه نمونه از این اینترفییس هم بسازین و توش از هندلر استفاده کنید تا تغییرات در مین ترد صورت بگیره و داخل هندلر بگین پروگرس ست بشه با عددی که فرستاده شده...

 
 YourWebserviceClass.Listener listener = new YourWebserviceClass.Listener() {

            @Override
            public void onDataReceive(float data) {
                G.HANDLER.post(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                
            }
        });
            }
            @Override
            public void onFail() {
Log.i("LOG", "Unknown exception!");
                }
        };

جایی که باید این لیسنر رو صدا بزنین و ازش استفاده کنین همون متد fd1 هست و باید هربار که بافر پر میشه عبارت cd1 رو به لیسنر پاس بدین(اون data که تو کد بالایی هست میتونه همون مقدار cd1 باشه) ، به صورتی ک زیر نوشتم میتونین cd1 رو بفرستین:

listener.onDataReceive(cd1);                   
0 0
ممنون ممکنه بیشتر توضیح بدید (8 سال پیش)
0 0
بله اما به نظرم بهتره که ویدیوی module downloader استاد رو مشاهده کنید تا کامل متوجه بشید ولی بازم اگه دسترسی به این ویدیو ندارین امر بفرمایید تا دوباره کامل تر بگم... (8 سال پیش)

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .