返回后恢复活动状态

时间:2016-07-26 12:42:31

标签: android activity-state

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

当我点击下载图标时,它会以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;
    }

}

1 个答案:

答案 0 :(得分:0)

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