我有这样的泼水活动。此活动显示1.5秒的映像,并从服务器下载所需的文件(如果不存在)。但我想延迟此页面,直到下载过程完成。否则只是像往常一样显示图像1秒。有人请帮忙。
编辑: 工作代码。 公共类Splash extends 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 );
}
}
}
请留下一些声誉。感谢。
答案 0 :(得分: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。