启动画面:使用处理程序

发布于 2024-10-18 02:46:23 字数 446 浏览 4 评论 0原文

我做对了吗? 我有一个启动屏幕(只是一个图像),并且 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

‖放下 2024-10-25 02:46:23

LONG_OPERATING_FUNCTION() 不应在主应用程序线程上完成,正如您在此处所做的那样。

理想情况下,您不使用启动屏幕,而只启用 MainActivity 的选定功能,同时在 AsyncTask 或其他内容中执行 LONG_OPERATING_FUNCTION()

如果有人用枪指着你的头并强迫你实现闪屏,以免你的大脑被溅到,我会这样做:

  • 消除你的 HandlerpostDelayed( ) 调用
  • 将其替换为 AsyncTask
  • AsyncTaskdoInBackground() 中,执行您的 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 your LONG_OPERATING_FUNCTION() in an AsyncTask 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:

  • Eliminate your Handler and postDelayed() call
  • Replace that with an AsyncTask
  • In doInBackground() of AsyncTask, do your LONG_OPERATING_FUNCTION()
  • If, when LONG_OPERATING_FUNCTION() is done, SPLASH_DISPLAY_LENGHT [sic] time has not elapsed, use SystemClock.sleep() to sleep for the remaining time (or not)
  • In onPostExecute(), start MainActivity and call finish()
注定孤独终老 2024-10-25 02:46:23
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        openingSound = MediaPlayer.create(Splash.this, R.raw.applause);
        openingSound.start();
        setContentView(R.layout.firstanimal);
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent openingSplash = new Intent("com.softech.LearnAnimal1.STARTINGPOINT");
                    startActivity(openingSplash);
                }
            }
        };

        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        openingSound.release();
        finish();

    }

这是一个完整的java代码,其中您将有openSound,并有5秒的休息时间,然后它将在您的菜单或第二个活动上移动,但请记住一件事,您还必须将带有意图过滤器的活动放入您的清单中: )
享受吧:)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        openingSound = MediaPlayer.create(Splash.this, R.raw.applause);
        openingSound.start();
        setContentView(R.layout.firstanimal);
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent openingSplash = new Intent("com.softech.LearnAnimal1.STARTINGPOINT");
                    startActivity(openingSplash);
                }
            }
        };

        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        openingSound.release();
        finish();

    }

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

诺曦 2024-10-25 02:46:23
Intent intent = new Intent(getApplicationContext(), AActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

通过使用getApplicationContext()的上下文就不会内存溢出;

公共类 RunnableActivity 扩展 Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("RunnableActivity onCreate");
    setContentView(R.layout.activity_main);
    mHandler.postDelayed(mRunnable, 3000);

}

@Override
protected void onResume() {
    super.onResume();
    System.out.println("RunnableActivity onResume");
}

@Override
protected void onPause() {
    super.onPause();
    System.out.println("RunnableActivity onPause");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    System.out.println("RunnableActivity onDestroy");
}

private Handler mHandler = new Handler(Looper.getMainLooper());

private Runnable mRunnable = new Runnable() {
    private WeakReference<Activity> weak = new WeakReference<Activity>(RunnableActivity.this);

    @Override
    public void run() {
        Activity a = weak.get();
        if (a != null) {
            Intent intent = new Intent(a.getApplicationContext(), AActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            a.getApplicationContext().startActivity(intent);
            a.finish();
        }
    }
};}
Intent intent = new Intent(getApplicationContext(), AActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

通过使用getApplicationContext()的context就不会内存溢出;

public class RunnableActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("RunnableActivity onCreate");
    setContentView(R.layout.activity_main);
    mHandler.postDelayed(mRunnable, 3000);

}

@Override
protected void onResume() {
    super.onResume();
    System.out.println("RunnableActivity onResume");
}

@Override
protected void onPause() {
    super.onPause();
    System.out.println("RunnableActivity onPause");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    System.out.println("RunnableActivity onDestroy");
}

private Handler mHandler = new Handler(Looper.getMainLooper());

private Runnable mRunnable = new Runnable() {
    private WeakReference<Activity> weak = new WeakReference<Activity>(RunnableActivity.this);

    @Override
    public void run() {
        Activity a = weak.get();
        if (a != null) {
            Intent intent = new Intent(a.getApplicationContext(), AActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            a.getApplicationContext().startActivity(intent);
            a.finish();
        }
    }
};}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文