0

我的ListView由一些音乐组成,每个项目旁边都有一个图标,如果音乐存在于 SD 卡中,则图标为播放图标,否则为下载图标。

当我单击下载图标时,它会以 a 开始下载,ProgressBar但如果我在下载时按后退按钮,然后再次返回活动,则进度条消失并且播放图标可见。

回到活动时,如何从上次更改中更新?

活动音乐:

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);

我的文件下载任务:

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;
    }

}
4

1 回答 1

0

当您玩活动时,请尝试保存状态并在活动恢复时恢复。检查https://stackoverflow.com/a/151940/1403009了解更多信息

于 2016-07-26T12:57:46.027 回答