【问题标题】:Kill intent on splash screen在启动画面上杀死意图
【发布时间】:2014-11-08 20:23:13
【问题描述】:

这是我的启动画面,如果我在 Intent 启动时按下 home 或 multitasking/appswitch 按钮,应用程序崩溃,在 logcat 中是 FATAL EXEPTION: Thread-1277。当玩家按下主页按钮时,我可以杀死/删除这个 Intent 吗?

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

【问题讨论】:

标签: android android-intent splash-screen multitasking


【解决方案1】:

以下代码跟踪SplashActivity 是否至少部分显示。如果是,它将继续MainActivity。如果没有(按返回按​​钮完成活动,按主页按钮停止活动)什么也不会发生。

此解决方案使用Fragments,因此时间可以保留在例如屏幕方向更改(无论您旋转设备多少次,它总是需要指定的时间 - 计时器不会重置)。

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 上测试的。它在按下 Home 或 Back 按钮时起作用。按下最近的应用程序按钮时它不起作用。我不知道您的用例,但就我个人而言,我希望该应用在我浏览最近的应用时继续启动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多