package com.quintet.littleweather.activity;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.quintet.littleweather.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class SplashActivity extends Activity {


    public static final int MSG_TIME_OUT = 1;
    public static final String DB_NAME = "china_city.db"; //数据库的名字
    public static final String PACKAGE_NAME = "com.quintet.littleweather"; //包名
    public static final String DB_PATH = "data/data/" + PACKAGE_NAME + "/" + DB_NAME;//db存放的路径


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        //在Splash界面等待三秒进入MainActivity
        waitWhile();


        //将放在assets文件夹里面的db数据库文件，copy到data/data/包名目录下
        copyDBToData();
    }


    private void copyDBToData() {
        File db = new File(DB_PATH);
        if (db.exists()) {
            return;
        } else {


            try {
                AssetManager assets = getAssets();

                InputStream is = assets.open(DB_NAME);

                FileOutputStream fos = new FileOutputStream(db);

                byte[] buf = new byte[1024];
                int len = 0;

                while ((len = is.read(buf)) != -1) {
                    fos.write(buf, 0, len);
                }

                is.close();
                fos.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


    private void waitWhile() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Message msg = myHandler.obtainMessage();
                msg.what = MSG_TIME_OUT;
                myHandler.sendMessage(msg);


            }
        }).start();
    }

    Handler myHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case MSG_TIME_OUT:
                    //enterMainActivity();
            }
        }
    };


    //等待MainActivity 写好后，进行耦合。。。
/*    private void enterMainActivity() {
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
    }*/
}

@@ -883,94 +883,281 @@
 vity
-%0A        waitWhile();%0A%0A%0A        //%E5%B0%86%E6%94%BE%E5%9C%A8assets%E6%96%87%E4%BB%B6%E5%A4%B9%E9%87%8C%E9%9D%A2%E7%9A%84db%E6%95%B0%E6%8D%AE%E5%BA%93%E6%96%87%E4%BB%B6%EF%BC%8Ccopy%E5%88%B0data/data/%E5%8C%85%E5%90%8D%E7%9B%AE%E5%BD%95%E4%B8%8B%0A
+,%E5%B9%B6%E5%B0%86%E6%94%BE%E5%9C%A8assets%E6%96%87%E4%BB%B6%E5%A4%B9%E9%87%8C%E9%9D%A2%E7%9A%84db%E6%95%B0%E6%8D%AE%E5%BA%93%E6%96%87%E4%BB%B6%EF%BC%8Ccopy%E5%88%B0data/data/%E5%8C%85%E5%90%8D%E7%9B%AE%E5%BD%95%E4%B8%8B%0A        waitWhile();%0A%0A    %7D%0A%0A%0A    private void waitWhile() %7B%0A        new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A%0A                //copy db to data/data/com.quintet.littleweather%0A  
         
 copy
@@ -1152,16 +1152,22 @@
         
+      
 copyDBTo
@@ -1174,21 +1174,416 @@
 Data();%0A
-    %7D
+%0A                myHandler.sendEmptyMessageDelayed(MSG_TIME_OUT, 3000);%0A%0A            %7D%0A        %7D).start();%0A    %7D%0A%0A    Handler myHandler = new Handler() %7B%0A        @Override%0A        public void handleMessage(Message msg) %7B%0A            super.handleMessage(msg);%0A%0A            switch (msg.what) %7B%0A                case MSG_TIME_OUT:%0A                    //enterMainActivity();%0A            %7D%0A        %7D%0A    %7D;
 %0A%0A%0A    p
@@ -1709,25 +1709,24 @@
   %7D else %7B%0A%0A
-%0A
             
@@ -2150,24 +2150,25 @@
 s.close();%0A%0A
+%0A
             
@@ -2268,775 +2268,8 @@
 %7D%0A%0A%0A
-    private void waitWhile() %7B%0A        new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    Thread.sleep(3000);%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A%0A                Message msg = myHandler.obtainMessage();%0A                msg.what = MSG_TIME_OUT;%0A                myHandler.sendMessage(msg);%0A%0A%0A            %7D%0A        %7D).start();%0A    %7D%0A%0A    Handler myHandler = new Handler()%7B%0A        @Override%0A        public void handleMessage(Message msg) %7B%0A            super.handleMessage(msg);%0A%0A            switch (msg.what) %7B%0A                case MSG_TIME_OUT:%0A                    //enterMainActivity();%0A            %7D%0A        %7D%0A    %7D;%0A%0A%0A
     

优化了一下SplashActivity ---BY Deng