启动画面:使用处理程序

时间:2011-02-17 21:19:36

标签: android handler

我做得对吗? 我有一个Splash屏幕(只是一个图像)和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真的没有完成。

3 个答案:

答案 0 :(得分:4)

LONG_OPERATING_FUNCTION()不应该在主应用程序线程上完成,就像你在这里一样。

理想情况下,您不使用启动画面,而只启用MainActivityLONG_OPERATING_FUNCTION()的所选功能,而AsyncTask或其他内容。

如果有人用枪瞄准你的头并强迫你实施防溅屏,以免你的大脑被呃,溅起来,我会这样做:

  • 取消HandlerpostDelayed()来电
  • 将其替换为AsyncTask
  • doInBackground()的{​​{1}}中,执行AsyncTask
  • 如果在完成LONG_OPERATING_FUNCTION()时,LONG_OPERATING_FUNCTION() [原文如此]时间尚未过去,请使用SPLASH_DISPLAY_LENGHT暂停一段时间(或不是)
  • SystemClock.sleep()中,启动onPostExecute()并致电MainActivity

答案 1 :(得分:1)

@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秒钟,然后你将继续你的菜单或第二个活动,但记住一件事你也必须把活动与意图过滤器放在一起你的清单:) 享受:)

答案 2 :(得分:1)

Intent intent = new Intent(getApplicationContext(), AActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

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

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