我有一个像这样的飞溅活动。此活动显示图像 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 );
}
}
}
请留下一些声誉。谢谢。