执行异步任务时出现启动画面
Splash screen while executing asynctask
我需要在我的应用程序中显示启动画面,但我不知道该怎么做。
我有一个 Main Activity,其中一些图像是通过调用另一个 class 中的函数从服务器下载的,我希望在图像准备好显示之前显示初始屏幕。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
//This is where the images are loaded
new ClasePeticionRest.CogerObjetosAleatoriosInicio(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
谢谢!!
创建启动画面 activity class 并创建 asyncTask Class,如下所示:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
在 doInBackground
中建立联系,然后在 onPostExecute
中写一个 intent 以转到 Main Activity 以显示图像:
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
这将为您完成工作
我需要在我的应用程序中显示启动画面,但我不知道该怎么做。 我有一个 Main Activity,其中一些图像是通过调用另一个 class 中的函数从服务器下载的,我希望在图像准备好显示之前显示初始屏幕。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
//This is where the images are loaded
new ClasePeticionRest.CogerObjetosAleatoriosInicio(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
谢谢!!
创建启动画面 activity class 并创建 asyncTask Class,如下所示:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
在 doInBackground
中建立联系,然后在 onPostExecute
中写一个 intent 以转到 Main Activity 以显示图像:
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
这将为您完成工作