package com.sinomaps.bjmapvr.ui;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;
import android.widget.Toast;
import com.lyt.baselib.utility.Utility;
import com.sinomaps.bjmapvr.R;
import com.sinomaps.bjmapvr.utility.MyUtility;
import java.lang.ref.WeakReference;
public class SplashActivity extends AppCompatActivity {
public static final int MAX_WAITING_TIME = 2000;
private boolean bAnimationEnd = false;
private boolean bCheckDataIsOK = false;
private MyHandler handler = new MyHandler(this);
static class MyHandler extends Handler{
WeakReference<SplashActivity> mActivity;
TextView mTextViewInfo;
MyHandler(SplashActivity activity){
mActivity = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
SplashActivity splashActivity = mActivity.get();
if(splashActivity == null) return;
mTextViewInfo = (TextView)mActivity.get().findViewById(R.id.textview_info);
switch(msg.what){
case 0:
mTextViewInfo.setText(msg.getData().getString("msg"));
break;
case -1://出现错误
mTextViewInfo.setText(msg.getData().getString("msg"));
Toast.makeText(splashActivity, msg.getData().getString("msg"), Toast.LENGTH_SHORT).show();
break;
}
}
}
private void sendMsg(String info,int what){
Message msg = handler.obtainMessage();
Bundle data = new Bundle();
data.putString("msg", info);
msg.setData(data);
msg.what = what;
handler.sendMessage(msg);
}
private void sendInfo(String info){
sendMsg(info, 0);
}
private void sendError(String info){
sendMsg(info, -1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = View.inflate(this, R.layout.activity_splash, null);
setContentView(view);
// 渐变展示启动屏
AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f);
aa.setDuration(MAX_WAITING_TIME);
aa.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
bAnimationEnd = true;
redirectTo();
}
});
view.startAnimation(aa);
//检查软件配套数据是否最新
new Thread(){
@Override
public void run(){
checkData();
}
}.start();
}
private void checkData() {
try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String oldVersion = prefs.getString("DataVersion", "0000-00-00");
String newVersion = getResources().getString(R.string.data_version);
if (oldVersion.compareTo(newVersion) < 0) {
sendInfo("正在初始化数据,请稍候...");
Utility.unzipAssetsDataToDisk(this, getString(R.string.data_name), MyUtility.getProjectBathPath(this));
sendInfo("数据初始化完毕。");
SharedPreferences.Editor editor = prefs.edit();
editor.putString("DataVersion", newVersion);
editor.apply();
}
} catch (Exception e) {
e.printStackTrace();
sendError("初始化数据出错");
return;
}
bCheckDataIsOK = true;
redirectTo();
}
private void redirectTo(){
//当动画和数据检查同时执行完后才跳转
if(bAnimationEnd && bCheckDataIsOK){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
}
Android启动界面渐变及数据初始化处理
最新推荐文章于 2021-05-31 11:49:45 发布