Android 数据库打包随APK发布

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


import com.android.qufu.dinner.MealActivityGroup;
import com.android.qufu.dinner.R;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


public class Loggin extends Activity {
	public static String dbName="dinner.db";//数据库的名字
    private static String DATABASE_PATH="/data/data/com.android.qufu.dinner/databases/";//数据库在手机里的路径
	 int alpha = 255; 
	 int b = 0; 
	
	 public void onCreate(Bundle savedInstanceState) { 
	        super.onCreate(savedInstanceState); 
	        setContentView(R.layout.login); 
	        //判断数据库是否存在
	        boolean dbExist = checkDataBase();
	        if(dbExist){
	        	
	        }else{//不存在就把raw里的数据库写入手机
	        	try{
	        		copyDataBase();
	        	}catch(IOException e){
	        		throw new Error("Error copying database");
	        	}
	        }
            
	        new Thread(new Runnable() { 
	            public void run() { 
	                initApp(); //初始化程序 
	                 
	                while (b < 2) { 
	                    try { 
	                        if (b == 0) { 
	                            Thread.sleep(20); 
	                            b = 1; 
	                        } else { 
	                            Thread.sleep(50); 
	                        } 
	                        updateApp(); 
	                    } catch (InterruptedException e) { 
	                        e.printStackTrace(); 
	                    } 
	                } 
	            } 
	        }).start(); 


	       
	    } 


	    public void updateApp() { 
	        alpha -= 5; 
	        if (alpha <= 0) { 
	            b = 2; 
	            if(true){
	            try{
	               Intent in = new Intent(Loggin.this,MealActivityGroup.class); 
	               Loggin.this.startActivity(in); 
	               Loggin.this.finish();
	            }catch(Exception e){
	            		
	            	}
	            }
	           
	        } 
	       
	    } 
	    /**
	     * 判断数据库是否存在
	     * @return false or true
	     */
	    public boolean checkDataBase(){
	    	SQLiteDatabase checkDB = null;
	    	try{
	    		String databaseFilename = DATABASE_PATH+dbName;
	    		checkDB =SQLiteDatabase.openDatabase(databaseFilename, null,
	    				SQLiteDatabase.OPEN_READONLY);
	    	}catch(SQLiteException e){
	    		
	    	}
	    	if(checkDB!=null){
	    		checkDB.close();
	    	}
	    	return checkDB !=null?true:false;
	    }
	    /**
	     * 复制数据库到手机指定文件夹下
	     * @throws IOException
	     */
	    public void copyDataBase() throws IOException{
	    	String databaseFilenames =DATABASE_PATH+dbName;
	    	File dir = new File(DATABASE_PATH);
	    	if(!dir.exists())//判断文件夹是否存在,不存在就新建一个
	    		dir.mkdir();
	    	FileOutputStream os = null;
	    	try{
	    		os = new FileOutputStream(databaseFilenames);//得到数据库文件的写入流
	    	}catch(FileNotFoundException e){
	    		e.printStackTrace();
	    	}
	    	InputStream is = Loggin.this.getResources().openRawResource(R.raw.test);//得到数据库文件的数据流
	        byte[] buffer = new byte[8192];
	        int count = 0;
	        try{
	        	
	        	while((count=is.read(buffer))>0){
	        		os.write(buffer, 0, count);
	        		os.flush();
	        	}
	        }catch(IOException e){
	        	
	        }
	        try{
	        	is.close();
	        	os.close();
	        }catch(IOException e){
	        	e.printStackTrace();
	        }
	    }
	    /**
	     * 初始化,这里是起始页的没有用
	     */
	    public void initApp(){          
	    } 
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值