2011-01-25 14 views
0

私はこのスニペットコードを使用して、サーバーからmp4ファイルをダウンロードしています。通常、ファイルは正しくダウンロードされますが、 がダウンロード処理を自動的に停止します。プログレスバーのストップをインクリメントします。ダウンロードを停止するカーネルプロセスはありますか?アンドロイドアプリケーションのサーバーからファイルをダウンロードする際の問題

//+++++++++= FUnction Call ++++++++++++++++ 

DOWNLOAD_FILE_NAME = "demo.mp4"; 
grabURL("mp4 file server url"); 

//+++++++++++++++++++++++++++++++++++++++++ 

public void grabURL(String url) { 

      new GrabURL().execute(url); 

    } 
private class GrabURL extends AsyncTask<String, Void, Void> { 
        private final HttpClient Client = new DefaultHttpClient(); 
        private String Content; 
        private String Error = null; 
        private ProgressDialog Dialog = new ProgressDialog(SlideShow.this); 

        protected void onPreExecute() { 


        showDialog(DIALOG_PROGRESS); 
       mProgressDialog.setProgress(0); 

        } 

      protected Void doInBackground(String... url) { 
       int count; 

       try { 
        URL url2 = new URL(url[0]); 
        URLConnection conexion = url2.openConnection(); 
        conexion.setUseCaches(true); 
        conexion.connect(); 

        // this will be useful so that you can show a tipical 0-100% progress bar 
        int lenghtOfFile = conexion.getContentLength(); 
        mProgressDialog.setMax(lenghtOfFile); 
        // downlod the file 
        InputStream input = new BufferedInputStream(url2.openStream()); 
        OutputStream output = new FileOutputStream(DOWNLOAD_FILE_NAME); 

        byte data[] = new byte[1024]; 

        while ((count = input.read(data)) != -1) { 

         mProgressDialog.incrementProgressBy(data.length); 
          output.write(data, 0, count); 
        } 

        output.close(); 
        input.close(); 
       } catch (Exception e) {} 
       return null; 
      } 

答えて

2

モバイルデバイスでは、インターネット接続は非常に信頼性が低いとみなされる必要があります。

コードにはダウンロード方法が示されていますが、接続管理はありません。 何らかの理由でインターネット接続が停止した場合、コードを再起動することはできません。

ConnectivityManager:http://developer.android.com/reference/android/net/ConnectivityManager.html のブロードキャストレシーバを使用して、接続が解除されたときに通知を受け、接続が再度アクティブになったときにそこからダウンロードを再起動することができます。

関連する問題