闪屏与后台任务

编程入门 行业动态 更新时间:2024-10-24 16:29:35
本文介绍了闪屏与后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图让我的应用程序launcha启动画面5秒钟,而在后台初始化各种网络服务。这里是我的code:

I am trying to make my application launcha splash screen for 5 seconds while initializing various web services in the background. Here is my code:

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Splash screen view setContentView(R.layout.splashscreen); final SplashScreen sPlashScreen = this; // The thread to wait for splash screen events mSplashThread = new Thread() { @Override public void run() { try { synchronized(this){ // Wait given period of time or exit on touch wait(5000); } } catch(InterruptedException ex) { } finally { finish(); // Run next activity Intent intent = new Intent(); intent.setClass(sPlashScreen, Splash_testActivity.class); startActivity(intent); stop(); } } }; mSplashThread.start(); for (int i=0;i<100;i++) Log.d("splash test", "initialize web methods"); }

现在我认为应该发生的是,在显示闪屏,应用程序应该日志初始化网络的方法。

Now what I think should happen is that while the splash screen is displayed, the application should log "initialize web methods."

但实际情况是,斜线屏幕上消失后,才日志添加。

But what actually happens is that the log is added only after the slash screen disappears.

我在做什么错了?

推荐答案

尝试去做的这样。本教程是简单,灵活。这就是你所需要的:

Try to do it this way. This tutorial is simple and flexible. This is what you need:

// You initialize _splashTime to any value // thread for displaying the SplashScreen Thread splashTread = new Thread() { @Override public void run() { try { int waited = 0; while(waited < _splashTime)) { sleep(100); waited += 100; } } } catch(InterruptedException e) { // do nothing } finally { finish(); startActivity(new Intent("com.droidnova.android.splashscreen.MyApp")); stop(); } } }; splashTread.start();

注意:此code从上面的URL采用

Note: This code is adopted from the above url.

更多推荐

闪屏与后台任务

本文发布于:2023-11-15 05:48:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591694.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!