2016-07-26 10 views
0

私のListViewは、各項目の横にあるアイコンがあります。音楽がSDカードに存在する場合、アイコンは再生アイコンです。それ以外の場合はダウンロードアイコンです。返された後にAcitivty状態を復元する

ダウンロードアイコンをクリックすると、ProgressBarがダウンロードされますが、ダウンロード中に戻るボタンを押して再びアクティビティに戻ると、プログレスバーが消えて再生アイコンが表示されます。

アクティビティに戻るときに最後に行った変更をどのように更新できますか?

ActivityMusic:

ActivityMusic.myFileDownloadTask task = new ActivityMusic.myFileDownloadTask(info, mAdapter, myList); 
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

マイアダプタ:

ActivityMusic.myFileDownloadTask task = new ActivityMusic.myFileDownloadTask(info, mAdapter, myList); 
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

myfileDownloadTask:

public static class myFileDownloadTask extends AsyncTask<String, Integer, String> { 
    //  private static final String TAG = FileDownloadTask.class.getSimpleName(); 
    final DownloadInfo mInfo; 
    public List<DownloadInfo> myList; 
    private String url = ""; 

    //  public DownloadInfoArrayAdapter downloadInfoArrayAdapter; 

    public myFileDownloadTask(DownloadInfo info, DownloadInfoArrayAdapter mAdapter, List<DownloadInfo> updateList) { 
     mInfo = info; 
     info.setDownloading(true); 
     mInfo.setDownloading(true); 

     mInfo.setDownloadState(DownloadInfo.DownloadState.DOWNLOADING); 
     //   downloadInfoArrayAdapter = mAdapter; 
     myList = updateList; 

    } 

    // 
    @Override 
    protected void onProgressUpdate(Integer... values) { 
     mInfo.setProgress(values[0]); 
     ProgressBar bar = mInfo.getProgressBar(); 
     if (bar != null) { 
      ActivityMusic.loading.setVisibility(View.INVISIBLE); 
      bar.setProgress(mInfo.getProgress()); 
      bar.invalidate(); 
     } 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     mInfo.setDownloadState(DownloadInfo.DownloadState.COMPLETE); 
     ProgressBar progressBar = mInfo.getProgressBar(); 
     progressBar.setVisibility(View.INVISIBLE); 
     mInfo.setDownloading(false); 
     ImageView img = mInfo.getDImageView(); 
     img.setImageResource(R.drawable.download); 
     mInfo.setDImaegView(img); 
     mInfo.getDImageView().setVisibility(View.INVISIBLE); 

     mInfo.setProgressBar(progressBar); 
     ImageView img2 = mInfo.getImageView(); 
     img2.setImageResource(R.drawable.play); 
     mInfo.setPImaegView(img2); 
     mInfo.getImageView().setEnabled(true); 
     mInfo.getImageView().setVisibility(View.VISIBLE); 

     mInfo.getImageView().setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //     Toast.makeText(ActivityMusic.mcontext, "Ari", Toast.LENGTH_SHORT).show(); 
       File mfolder = new File(Environment.getExternalStorageDirectory() + "/Sh_M/" + mInfo.getFilename() + ".mp3"); 
       DownloadInfoArrayAdapter.mUri = Uri.fromFile(mfolder); 
       ActivityMusic.audioWife.getInstance().release(); 
       ActivityMusic.audioWife 
         .init(null, DownloadInfoArrayAdapter.mUri) 
         .setPlayView(ActivityMusic.mPlayMedia) 
         .setPauseView(ActivityMusic.mPauseMedia) 
         .setSeekBar(ActivityMusic.mMediaSeekBar) 
         .setRuntimeView(ActivityMusic.mRunTime) 
         .setTotalTimeView(ActivityMusic.mTotalTime).play(); 
       ActivityMusic.relativeLayout.setVisibility(View.VISIBLE); 
       ActivityMusic.isPlaying = true; 
      } 
     }); 
     //   downloadInfo.add(new DownloadInfo("", 1000, "")); 
     //   myList.add(ActivityMusic.downloadInfo.get(0)); 
     //   downloadInfoArrayAdapter = new DownloadInfoArrayAdapter(mcontext,R.id.list_view,downloadInfo); 
     //   listView.setAdapter(downloadInfoArrayAdapter); 
     downloadInfoArrayAdapter.notifyDataSetChanged(); 
     //   ActivityMusic.downloadInfoArrayAdapter.notifyDataSetChanged(); 

    } 

    @Override 
    protected void onPreExecute() { 
     mInfo.setDownloadState(DownloadInfo.DownloadState.DOWNLOADING); 
     mInfo.setDownloading(true); 
    } 


    @Override 
    protected String doInBackground(String... f_url) { 
     //   mInfo.setDownloadState(DownloadInfo.DownloadState.DOWNLOADING); 
     int count; 
     try { 
      URL url = new URL(mInfo.getLink()); 
      URLConnection conection = url.openConnection(); 
      conection.connect(); 
      // getting file length 
      int lenghtOfFile = conection.getContentLength(); 

      // input stream to read file - with 8k buffer 
      InputStream input = new BufferedInputStream(url.openStream(), 8192); 

      File folder = new File(Environment.getExternalStorageDirectory() + "/Sh_M"); 
      if (!folder.exists()) { 
       folder.mkdir(); 
      } 
      // Output stream to write file 
      OutputStream output = new FileOutputStream("/sdcard/Sh_M/" + mInfo.getFilename() + ".mp3"); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       // After this onProgressUpdate will be called 
       publishProgress((int) ((total * 1000)/lenghtOfFile)); 

       // writing data to file 
       output.write(data, 0, count); 
      } 

      // flushing output 
      output.flush(); 


      // closing streams 
      output.close(); 
      input.close(); 

     } catch (Exception e) { 
      Log.e("Error: ", ""); 
     } 
     return null; 
    } 

} 
+0

IMHOサービスを使用してダウンロードし、進行状況バーにそのサービスのステータスを表示させてください。 –

+0

サービスから読む方法 – DavidOli

+0

[retrofit](http://square.github.io/retrofit/)、さらには[OkHttp](http://square.github.io/okhttp/)のようなものを使用してください。 –

答えて

関連する問題