gpt4 book ai didi

java - 如何延迟页面或 sleep 线程直到下载过程或进度条完成

转载 作者:行者123 更新时间:2023-12-01 14:10:22 24 4
gpt4 key购买 nike

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

编辑:工作代码。 公共(public)类 Splash 扩展 Activity{ boolean 标志 = 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 );
}
}
}

请留下一些声誉。谢谢。

最佳答案

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

int numCompletedDownloads = 0;

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

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

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 .

关于java - 如何延迟页面或 sleep 线程直到下载过程或进度条完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18563330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com