【问题标题】:Splash screen while executing asynctask执行异步任务时的闪屏
【发布时间】:2017-11-12 20:57:23
【问题描述】:

我需要在我的应用程序中显示启动画面,但我不知道该怎么做。 我有一个 Main Activity,其中通过调用另一个类中的函数从服务器下载了一些图像,并且我希望在图像准备好显示之前显示初始屏幕屏幕。

这是我的代码:

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);

}

谢谢!!

【问题讨论】:

  • 1.显示启动画面。 2. 运行异步任务。 3. 在它的 postExecute() 上使图像可见并且启动视图消失。

标签: android splash-screen


【解决方案1】:

创建一个启动活动类并创建如下所示的 asyncTask 类:

 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 intent=new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);

这将为您完成工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    相关资源
    最近更新 更多