0

我有一个像这样的飞溅活动。此活动显示图像 1.5 秒,如果不存在,则从服务器下载所需文件。但我想延迟这个页面,直到下载过程完成。否则像往常一样只显示图像 1 秒。有人请帮忙。

编辑: 工作代码。公共类 Splash 扩展 Activity{ boolean flag = true;

    @Override
    protected void onCreate(Bundle LoadingStartPage) {
        // TODO Auto-generated method stub
        super.onCreate(LoadingStartPage);
        setContentView(R.layout.splash);
        File file_path = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/images/");

        if(file_path.exists()){

        }else{
            String url = "http://example.com";
            new DownloadTask().execute( url );
        }
        Thread timer = new Thread(){
            public void run(){
                try{
                    while(flag){
                    sleep(1500);
                    }
                } catch (InterruptedException e){
                    e.printStackTrace();
                } finally {
                    Intent openMainActivity = new Intent("com.kabe.example.MAINACTIVITY");
                    startActivity(openMainActivity);
                }
            }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }


    /**
     * To download and unzip the files from API
     */
    protected ProgressDialog mProgressDialog;

    //Background task to download and unpack zip file in background.
    private class DownloadTask extends AsyncTask<String,Void,Exception> {

        @Override
        protected void onPreExecute() {
            showProgress();
        }

        @Override
        protected Exception doInBackground(String... params) {
            String url = (String) params[0];
            try {
                downloadAllAssets(url);
            } catch ( Exception e ) { return e; }
            return null;
        }

        @Override
        protected void onPostExecute(Exception result) {
            dismissProgress();
            flag = false;
            if ( result == null ) { return; }
            // something went wrong, post a message to user - you could use a dialog here or whatever
            Toast.makeText(Splash.this, result.getLocalizedMessage(), Toast.LENGTH_LONG ).show();
        }
    }


    //Progress window
    protected void showProgress( ) {
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setTitle( R.string.progress_title );
        mProgressDialog.setMessage( getString(R.string.progress_detail) );
        mProgressDialog.setIndeterminate( true );
        mProgressDialog.setCancelable( false );
        mProgressDialog.show();
    }

    protected void dismissProgress() {
        // You can't be too careful.
        if (mProgressDialog != null && mProgressDialog.isShowing() && mProgressDialog.getWindow() != null) {
            try {
                mProgressDialog.dismiss();
            } catch ( IllegalArgumentException ignore ) { ; }
        }
        mProgressDialog = null;
    }

    //Download zip file specified by url, then unzip it to a folder in external storage.
    private void downloadAllAssets( String url ) {
        // Temp folder for holding asset during download
        File zipDir =  ExternalStorage.getSDCacheDir(this, "tmp");
        // File path to store .zip file before unzipping
        File zipFile = new File( zipDir.getPath() + "/temp.zip" );
        // Folder to hold unzipped output
        File outputDir = ExternalStorage.getSDCacheDir( this, "images" );

        try {
            DownloadFile.download( url, zipFile, zipDir );
            unzipFile( zipFile, outputDir );
        }
    }   
}

请留下一些声誉。谢谢。

4

1 回答 1

1

听起来像是一个非常标准的并发问题。通常你会有一些标志或计数器:

int numCompletedDownloads = 0;

// this could be 0, 1 or 2 depending on what files exist locally
int numDownloadsToExecute; 

在你的 try-catch 块中,你会有这样的东西:

Thread timer = new Thread(){
            public void run(){
                try{
                    while (numCompletedDownloads < numDownloadsToExecute) {
                        sleep(100);
                    }
                } catch (InterruptedException e){
                    e.printStackTrace();
                } finally {
                    Intent openMainActivity = new Intent("com.kabe.sample.MAINACTIVITY");
                    startActivity(openMainActivity);
                }
            }
        };

然后,在您的 DownloadTask 中,您将增加计数器。

    @Override
    protected void onPostExecute(Exception result) {
            dismissProgress();
            numCompletedDownloads++;
            // other stuff..
    }

当然,问题是numCompletedDownloads变量的并发修改是非常危险的,并且可能导致数据竞争。为避免这种情况,您需要研究某种锁定机制或某种形式的原子操作。考虑也许使用AtomicInteger

于 2013-09-01T22:12:52.223 回答