2011-07-28 16 views
1

私は現在、URLからmp3をダウンロードすると消えるステータスバー通知を持っています。それは正常に起動します(そして、ファイルは正常にダウンロードされます)が、進捗バーは進まない。それは単にそのままです。私はコードをどこに置くべきかわからないのでダウンロードの進捗状況を通知します。何か案は?Android-status bar notification with progress bar

ありがとうございました。ここで

は、私のコードの一部です:

public class DownloadFile extends AsyncTask<String, Integer, String>{ 
     @Override 
     protected String doInBackground(String... url) { 
      try { 
       URL url2 = new URL(sdrUrl); 
       HttpURLConnection c = (HttpURLConnection) url2.openConnection(); 
       c.setRequestMethod("GET"); 
       c.setDoOutput(true); 
       c.connect(); 

       int lengthOfFile = c.getContentLength(); 

       String PATH = Environment.getExternalStorageDirectory() 
         + "/download/"; 
       Log.v("", "PATH: " + PATH); 
       File file = new File(PATH); 
       file.mkdirs(); 

       String [] path = url2.getPath().split("/"); 
       String mp3 = path [path.length-1]; 
       String sdrMp3 = mp3.replace("%20", ""); 

       File outputFile = new File(file, sdrMp3); 
       FileOutputStream fos = new FileOutputStream(outputFile); 

       InputStream is = c.getInputStream(); 

       byte[] buffer = new byte[1024]; 
       int len1 = 0; 
       while ((len1 = is.read(buffer)) != -1) { 
        publishProgress((int)(len1*100/lengthOfFile)); 
        fos.write(buffer, 0, len1); 
       } 
       fos.close(); 
       is.close(); 

       }catch (IOException e) { 
         e.printStackTrace(); 
       } 


      return null; 
     } 

     @Override 
     public void onProgressUpdate(Integer... values) { 


      super.onProgressUpdate(values); 
     } 



    } 

    public void DownloadNotification(){ 

     Intent intent = new Intent(this, BlogActivity.class); 
     final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); 

     final Notification notification = new Notification(R.drawable.sdricontest, "Downloading...", System 
       .currentTimeMillis()); 
     notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 
     notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout); 
     notification.contentIntent = pendingIntent; 
     notification.contentView.setImageViewResource(R.id.download_icon, R.drawable.sdricontest); 
     notification.contentView.setTextViewText(R.id.download_text, "simulation in progress"); 
     notification.contentView.setProgressBar(R.id.download_progress, 100, progress, false); 

     getApplicationContext(); 
     final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
       Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(42, notification); 
    } 

} 

答えて

-1

は、以下のことを試してみてください -

public class DownloadFile extends AsyncTask<String, Integer, String>{ 
    ProgressDialog pd; 

    public DownloadFile() { 
    super(); 
    pd = ProgressDialog.show(context, "Loading..", "Doing Stuff", true,false); 
    } 

    @Override 
    protected String doInBackground(String... url) { 
     //stuff 
    } 


    @Override 
    protected void onPostExecute (Boolean result){ 

    pd.dismiss(); 

    } 


} 
+0

これはちょうどANR私のアプリです。ユーザーがボタンを押すとasynctaskとダウンロード通知が実行されます。 – Splitusa

+0

コンストラクタとonPostExecuteを追加しました。これは自分のコードからリッピングして動作します。 – Suchi

+0

ステータスバーの通知ではなく、画面に表示されるプログレスバーの読み込みバーを使用しています... – Splitusa