启动画面:使用处理程序
我做对了吗? 我有一个启动屏幕(只是一个图像),并且 onCreate()
我在运行繁重的函数后启动主要活动:
SPLASH_DISPLAY_LENGHT=2500;
new Handler().postDelayed(new Runnable(){
public void run() {
LONG_OPERATING_FUNCTION();
Intent mainIntent = new Intent(this, MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
我认为我有内存泄漏,我正在尝试找到它。 我认为 Splash 还没有真正结束。
Am I doing it right?
I have a Splash screen (just an image), and onCreate()
I start the main activity after running a heavy function:
SPLASH_DISPLAY_LENGHT=2500;
new Handler().postDelayed(new Runnable(){
public void run() {
LONG_OPERATING_FUNCTION();
Intent mainIntent = new Intent(this, MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
I think I have a memory leak, and I'm trying to find it.
I don't think the Splash really is finishing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LONG_OPERATING_FUNCTION()
不应在主应用程序线程上完成,正如您在此处所做的那样。理想情况下,您不使用启动屏幕,而只启用
MainActivity
的选定功能,同时在AsyncTask
或其他内容中执行LONG_OPERATING_FUNCTION()
。如果有人用枪指着你的头并强迫你实现闪屏,以免你的大脑被溅到,我会这样做:
Handler
和postDelayed( )
调用AsyncTask
AsyncTask
的doInBackground()
中,执行您的LONG_OPERATING_FUNCTION()
LONG_OPERATING_FUNCTION()
完成后,SPLASH_DISPLAY_LENGHT
[sic] 时间尚未过去,请使用SystemClock.sleep()
休眠剩余时间(或没有)onPostExecute()
中,启动MainActivity
并调用finish()
LONG_OPERATING_FUNCTION()
should not be done on the main application thread, as you have it here.Ideally, you do not use a splash screen, but rather only enable selected features of
MainActivity
while do yourLONG_OPERATING_FUNCTION()
in anAsyncTask
or something.If somebody is pointing a gun at your head and forcing you to implement a splash screen lest it be your brains that get, er, splashed, I would do this:
Handler
andpostDelayed()
callAsyncTask
doInBackground()
ofAsyncTask
, do yourLONG_OPERATING_FUNCTION()
LONG_OPERATING_FUNCTION()
is done,SPLASH_DISPLAY_LENGHT
[sic] time has not elapsed, useSystemClock.sleep()
to sleep for the remaining time (or not)onPostExecute()
, startMainActivity
and callfinish()
这是一个完整的java代码,其中您将有openSound,并有5秒的休息时间,然后它将在您的菜单或第二个活动上移动,但请记住一件事,您还必须将带有意图过滤器的活动放入您的清单中: )
享受吧:)
This is a complete java code in this u'll have openingSound with 5 seconds break and then u it'll move on your menu or second activity but remeber one thing u also have to put activity with intent filters in your manifest :)
Enjoy :)
通过使用getApplicationContext()的上下文就不会内存溢出;
公共类 RunnableActivity 扩展 Activity {
通过使用getApplicationContext()的context就不会内存溢出;
public class RunnableActivity extends Activity {