要实现的逻辑是这样的,首先运行App去到欢迎页面,在欢迎页面判断两小时之内有没有登陆过,有就不用验证登陆,直接到主页,没有就跳登陆页面
在这过程中用到了一个:SharedPreferences
welcome的布局页面:就一张欢迎图片
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/mainpage"
android:id="@+id/main">
</RelativeLayout>
activity
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
public class WelcomePage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
// 延迟几秒,然后跳转到登录页面
new Handler().postDelayed(r, 3000);
}
Runnable r = new Runnable() {
@Override
public void run() {
SharedPreferences preferences=getSharedPreferences("user",MODE_PRIVATE);
String Status=preferences.getString("Status", "0");
Intent intent = new Intent();
if(Status=="0"){
intent.setClass(WelcomePage.this, MainActivity.class); //去登录页面
}else{
Method method= new Method();
//Date dafultDate=new Date("1900-1-1");
boolean isLogin=method.DateCompare(preferences.getString("LastDate", "1900-1-1 00:00:00"));
if(!isLogin){
intent.setClass(WelcomePage.this, MainActivity.class); //去登录页面
}else{
//记录当前时间
SimpleDateFormat formatter=new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
Date curDate=new Date(System.currentTimeMillis());
String strDate= formatter.format(curDate);
Editor editor=preferences.edit();
editor.putString("LastDate", strDate);
editor.commit();
intent.setClass(WelcomePage.this, TextWebWiew.class); //去首页
}
}
startActivity(intent);
finish();
}
};
}