2014-11-08 81 views
0

这是我的启动画面,如果我在按下主页或多任务/应用程序切换按钮时,意图启动应用程序崩溃,在logcat是致命的EXEPTION:螺纹1277。当玩家按主页按钮时,我可以杀死/删除这个意图吗?意图启动画面

public class SplashScreen extends Activity { 
private static int loadingTime = 1000; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    setVolumeControlStream(AudioManager.STREAM_MUSIC); 

    setContentView(R.layout.loading_screen); 

    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Intent i = new Intent(SplashScreen.this, MainActivity.class); 
      startActivity(i); 
      finish(); 
     } 
    }, loadingTime); 
} 
} 
+0

上帝的方式告诉你,splashscreens是邪恶的。 http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/ – Simon 2014-11-08 20:40:30

回答

0

以下代码跟踪SplashActivity是否至少部分显示。如果是的话,它将继续到MainActivity。如果没有(按Back按钮完成活动,则通过按Home按钮停止活动),则不会发生任何事情。

该解决方案使用Fragment s,因此时间跨越例如,屏幕方向改变(无论您旋转设备多少次,它都会持续指定的时间 - 定时器不会重置)。

public class SplashActivity extends Activity { 
    // tracks when the activity is at least partially visible (e.g. under a dialog) 
    private boolean mStarted = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // your current code 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    setVolumeControlStream(AudioManager.STREAM_MUSIC); 
    setContentView(R.layout.activity_startup); 

    if (savedInstanceState == null) { 
     // first time onCreate, create fragment which starts countdown 
     getFragmentManager() 
      .beginTransaction() 
      .add(SplashFinishFragment.newInstance(), SplashFinishFragment.TAG) 
      .commit(); 
    } else { 
     // fragment already set up from first onCreate after screen rotation 
    } 
    } 

    @Override 
    protected void onStart() { 
    // the activity becomes at least partially visible 
    mStarted = true; 

    super.onStart(); 
    } 

    @Override 
    protected void onStop() { 
    // the activity is no longer visible 
    mStarted = false; 

    super.onStop(); 
    } 

    public boolean isStarted2() { 
    // there is already hidden method isStarted() in the framework 
    // you can't use it and are not allowed to override it 
    return mStarted; 
    } 

    public static class SplashFinishFragment extends Fragment { 
    private static final String TAG = SplashFinishFragment.class.getSimpleName(); 

    private static final int DELAY = 1000; // one second delay 

    private static final Handler mHandler = new Handler(); // one main thread anyway 

    private final Runnable mRunnable = new Runnable() { 
     @Override 
     public void run() { 
     if (getActivity() == null) { 
      // this should never happen, there is no activity, so no fragment 
      Log.e(TAG, "No activity!"); 
      return; 
     } 

     SplashActivity a = (SplashActivity) getActivity(); 

     if (a.isStarted2() || a.isChangingConfigurations()) { 
      // if activity is even partially visible or is rotating screen right now, continue 
      Intent i = new Intent(a, SettingsActivity.class); 
      a.startActivity(i); 
     } 

     // in any case close splash 
     a.finish(); 
     } 
    }; 

    public static SplashFinishFragment newInstance() { 
     return new SplashFinishFragment(); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // the countdown will continue (not reset) across screen rotations 
     setRetainInstance(true); 

     // try running the main activity after specified time 
     mHandler.postDelayed(mRunnable, DELAY); 
    } 

    @Override 
    public void onDestroy() { 
     // if the fragment gets destroyed (e.g. activity closes) do not launch main activity 
     mHandler.removeCallbacks(mRunnable); 

     super.onDestroy(); 
    } 
    } 
} 

这是在虚拟Galaxy S2上测试的。当按下主页或后退按钮时,它将起作用。按下最近应用按钮时它不起作用。我不知道你的使用案例,但我个人认为,当我浏览最近的应用时,应用会继续启动。