2011-09-28 17 views
6

अपडेट कैसे कर सकता है मैं एक वेबपृष्ठ को पार्स करना चाहता हूं और प्रगतिशील शैली को क्षैतिज दृश्य करना चाहता हूं और बाइट को बाइट बढ़ा सकता हूं, यह संभव है? इस तरहएंड्रॉइड: AsyncTask, प्रोग्रेसडिअलॉग वृद्धि

+0

हाँ, यह संभव http://goo.gl/eeiu5 – Matthieu

+0

है एक उदाहरण नहीं है कृपया? – MimmoG

+0

इस उदाहरण का संदर्भ लें लिंक: https://sites.google.com/site/androidhowto/how-to-1/create-a-custom-progress-bar-using-asynctask – Uttam

उत्तर

20

कोशिश कुछ,

एक ProgressDialog बनाएँ।

ProgressDialog mProgressDialog = new ProgressDialog(Your_Activity.this); 
mProgressDialog.setMessage("Here you can set a message"); 
mProgressDialog.setIndeterminate(false); 
mProgressDialog.setMax(100); 
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
mProgressDialog.show(); 
MyAsyncTask obj = new MyAsyncTask(); 
obj.execute("url"); 

आपका AsyncTask क्लास।

private class MyAsyncTask extends AsyncTask<String, Integer, String>{ 
    @Override 
    protected String doInBackground(String... url) { 
     int count; 
     try { 
      URL url = new URL(url[0]); 
      URLConnection connection = url.openConnection(); 
      connection.connect(); 
      // this will be useful so that you can show a tipical 0-100% progress bar 
      int length = connection.getContentLength(); 

      // downlod the file 
      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream("/sdcard/file_name.txt"); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       publishProgress((int)(total*100/length)); 
       output.write(data, 0, count); 
      } 
      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (Exception e) {} 
     return null; 
    } 
    @Override 
    public void onProgressUpdate(String... args){ 
     mProgressDialog.setProgress(args[0]); 
    } 
    } 
} 

आप AndroidManifest फ़ाइल में इन अनुमति के देने के लिए किया है।

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

कुछ यूआरएल में आपको हमेशा -1 मिल जाएगा, फिर इस कोड का उपयोग करें HttpURLConnection कनेक्शन = (HttpURLConnection) url.openConnection(); \t \t connection.setRequestMethod ("HEAD"); \t \t कनेक्शन.कनेक्ट(); \t \t int लंबाई = connection.getContentLength(); –

संबंधित मुद्दे