使用Splash界面前,新建一个Activity(注意在清单文件中注册)。并将入口Activity设置到此Activity(因为第一个显示的就是它嘛!),即将
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
此段剪切到此Activity的节点下。
A.定义一个布局文件,此文件布局主要为静态,一般不需要业务逻辑控制。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/easytodo" />
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的应用"
android:textColor="@color/white"
android:textSize="35sp" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:id="@+id/version"
android:text="版本号"
/>
</LinearLayout>
①一般来说,Splash界面都是全屏的。此处有两种解决办法。
1、在清单文件中将Activity的样式设置为@android:style/Theme.NoTitleBar.Fullscreen
即
android:name="包名.类名"
android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
2、另外一种就是在界面中使用代码控制
requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//取消状态栏
在Activity中onCreate方法中,setContentView()调用之前加入前面两句。
②在首次打开界面时,如果设置一个特效的话,会给用户一个良好的第一印象。
此处使用了一个两秒钟的渐变特效。
AlphaAnimation animation = new AlphaAnimation(0.5F, 1.0F);
animation.setDuration(2000);
使用此特效的方法很简单。将需要设置特效的控件找到,比如这里为布局文件的根节点一个线性布局设置了一个id。在Activity中找到这个LinearLayout。
LinearLayout layout = (LinearLayout) this.findViewById(R.id.splash);
layout.setAnimation(animation);
③然后使用一个多线程,开启一个新线程执行网络连接操作。在此之前。使用一个工具类NetTools判断网络连接状况。
如果网络连接正常,则执行登录操作。登录操作需要从本地SharedPrefrences中读取以前登录用户的用户名和密码。
user = MyApp.sp.getString("loginName", null);
password = MyApp.sp.getString("passWord", null);
判断是否其数据存在。如果存在的话,执行登录操作。因为在此线程中使用了消息处理器Handler.在需要执行的语句使用looper.prepare()和looper.loop()包裹一下。
RequestParams params = new RequestParams();
params.addBodyParameter("loginName", user);
params.addBodyParameter("passWord", password);
Looper.prepare();
ConnHelper.CreateConn(MyApp.LOGIN, params, handler, 1);
Looper.loop();
④所有消息接收都到handler中处理。
a、如果成功登录。解析返回的数据,将部分内容持久化到SharedPrefrences当中。
editor.putString("loginName",
MyApp.user.getLoginName());
editor.putString("passWord",
MyApp.user.getPassWord());
editor.putString("nickName",
MyApp.user.getNickName());
editor.putString("phone", MyApp.user.getPhone());
editor.putInt("id", MyApp.user.getId());
editor.putInt("groupId", MyApp.user.getGroupId());
editor.putString("groupName",
MyApp.user.getGroupName());
editor.commit();
JPushInterface.setAlias(SplashActivity.this,
MyApp.user.getLoginName(), null);
然后跳转到MainActivity中。
private void LoadMain() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
JPushInterface.init(SplashActivity.this);
startActivity(intent);
finish();
}
b、如果登录不成功。则跳转到MainActivity的同时清除保存在SharedPrefrences中的数据。
} else {
// 如果登录不成功,则将SharedPreferences中数据清空
clearSp();
LoadMain();
}