1.第一个启动页(WelcomeActivity )
public class WelcomeActivity extends Activity {
private final MyHandler myHandler = new MyHandler(this);
private static class MyHandler extends Handler {
private final WeakReference<WelcomeActivity> mActivity;
public MyHandler(WelcomeActivity activity) {
this.mActivity = new WeakReference<WelcomeActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
WelcomeActivity welcomeActivity = mActivity.get();
if (welcomeActivity != null) {
switch (msg.what) {
case 1000:
goMainActivity(welcomeActivity);
break;
case 1001:
goLoginActivity(welcomeActivity);
break;
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT,如果activity在task存在,拿到最顶端,不会启动新的Activity
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
//完美解决:APP下载安装后,点击“直接打开”,启动应用后,按下HOME键,再次点击桌面上的应用,会重启一个新的应用问题
finish();
return;
}
init();
}
private void init() {
//判断应用是否登录
if (SharePreferenceXutil.getIsOrNotLogin()) {
// 使用Handler的postDelayed方法,1秒后执行跳转到MainActivity
myHandler.sendEmptyMessageDelayed(1000, 1000);
} else {
myHandler.sendEmptyMessageDelayed(1001, 1000);
}
}
private static void goMainActivity(WelcomeActivity mainActivity) {
Intent intent = new Intent(mainActivity, MainActivity.class);
mainActivity.startActivity(intent);
mainActivity.finish();
}
private static void goLoginActivity(WelcomeActivity mainActivity) {
Intent intent = new Intent(mainActivity, LoginActivity.class);
mainActivity.startActivity(intent);
mainActivity.finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 不允许返回
if (keyCode == KeyEvent.KEYCODE_BACK) {
return false;
}
return super.onKeyDown(keyCode, event);
}
}
2.AndroidManifest.xml里的注册
<activity
android:name=".ui.WelcomeActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.MainActivity"
android:launchMode="singleTask"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan" />
<activity android:name=".ui.login.LoginActivity" />