Android启动界面渐变及数据初始化处理

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值