Android开发:启动应用的数据库初始化和版本更新


  1. public class SplashActivity extends Activity {  
  2.     private static final String TAG = "mainactivity";  
  3.     private static final int NEED_UPDATE = 1;  
  4.     private static final int CONNECT_SERVER_ERROR = 2;  
  5.     private static final int SHOW_MESSAGE=3;  
  6.     private static final int HIDE_MESSAGE=4;  
  7.       
  8.     UpdateInfo info;  
  9.     AddressService addressService;  
  10.     TextView tv_copydb;  
  11.     private Handler handler = new Handler() {  
  12.         @Override  
  13.         public void handleMessage(Message msg) {  
  14.             super.handleMessage(msg);  
  15.             switch (msg.what) {  
  16.             case NEED_UPDATE:  
  17.                 showUpdateDialog();  
  18.                 break;  
  19.             case CONNECT_SERVER_ERROR:  
  20.                 showErrorDialog();  
  21.                 break;  
  22.             case SHOW_MESSAGE:  
  23.                 tv_copydb.setVisibility(View.VISIBLE);  
  24.                 break;  
  25.             case HIDE_MESSAGE:  
  26.                 tv_copydb.setVisibility(View.INVISIBLE);  
  27.                 break;  
  28.             }  
  29.         }  
  30.         private void showErrorDialog() {  
  31.             AlertDialog.Builder builder = new Builder(SplashActivity.this);  
  32.             builder.setTitle("错误");  
  33.             builder.setMessage("连接服务器失败");  
  34.             builder.setPositiveButton(R.string.ok, new OnClickListener() {  
  35.   
  36.                 public void onClick(DialogInterface dialog, int which) {  
  37.                     //finish();   
  38.                     //临时修改,不连接服务器也能进程序主界面   
  39.                     Intent intent = new Intent(SplashActivity.this,MainScreenActivity.class);  
  40.                     startActivity(intent);  
  41.                     finish();  
  42.                 }  
  43.             });  
  44.             builder.create().show();  
  45.         }  
  46.         /*** 
  47.          * 因为在非UI线程里面不能创建对话框,所以我们需要用handler 和 message 来实现创建对话框 
  48.          * */  
  49.         private void showUpdateDialog() {  
  50.             AlertDialog.Builder builder = new Builder(SplashActivity.this);  
  51.             //错误AlertDialog.Builder builder = new Builder(getApplicationContext());   
  52.             //mainactivity.this 是 getApplicationContext()的子集.   
  53.             //mainactivity 里面有一些参数 比如说当前activity 的windowlayout的一些参数是 getApplicationContext()所没有的   
  54.             builder.setTitle(R.string.please_update);  
  55.             builder.setMessage(info.getDescription());  
  56.             builder.setPositiveButton(R.string.ok, new OnClickListener() {  
  57.                 public void onClick(DialogInterface dialog, int which) {  
  58.                     Log.i(TAG, "调用下载方法");  
  59.                     final ProgressDialog pd =  new ProgressDialog(SplashActivity.this);  
  60.                     pd.setMessage(getApplicationContext().getResources().getString(R.string.downing));  
  61.                     pd.show();  
  62.                     try {  
  63.                         new Thread(){  //开辟线程下载服务器更新的apk文件   
  64.                             public void run() {  
  65.                                 File file;  
  66.                                 try {  
  67.                                     file = DownLoadService.getFile(info.getUrl()); //解析的url   
  68.                                     install(file);  
  69.                                     pd.dismiss();  
  70.                                 } catch (Exception e) {  
  71.                                     e.printStackTrace();  
  72.                                 }  
  73.                             }}.start();  
  74.   
  75.                     } catch (Exception e) {  
  76.                         e.printStackTrace();  
  77.                     }  
  78.                 }  
  79.                   
  80.                 //确定了后开始安装,替换掉原来的了   
  81.                 private void install(File file) {  
  82.                     Intent intent = new Intent(Intent.ACTION_VIEW);  
  83.                     intent.setDataAndType(Uri.fromFile(file), "application/vnd.Android.package-archive");  
  84.                     finish();               //自己安装完了关闭AlertDialog   
  85.                     startActivity(intent);    
  86.                 }  
  87.             });  
  88.             builder.setNegativeButton(R.string.cancle, new OnClickListener() {  
  89.   
  90.                 public void onClick(DialogInterface dialog, int which) {  
  91.                     Log.i(TAG, "用户取消");  
  92.                     Intent intent = new Intent(SplashActivity.this,MainScreenActivity.class);  
  93.                     startActivity(intent);  
  94.                     finish();  
  95.                 }  
  96.             });  
  97.             AlertDialog dialog = builder.create();  
  98.             dialog.show();  
  99.         }  
  100.     }; //handler结束   
  101.       
  102.     /** Called when the activity is first created. */  
  103.     @Override  
  104.     public void onCreate(Bundle savedInstanceState) {  
  105.         super.onCreate(savedInstanceState);  
  106.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  107.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  108.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  109.         setContentView(R.layout.splashactivity);  
  110.         tv_copydb =  (TextView) findViewById(R.id.copydatabase);  
  111.   
  112.         addressService = new AddressService(this);  
  113.         boolean isexist = addressService.isExist();  
  114.         if(isexist){//数据库存在不做任何操作   
  115.             new Thread(new CheckVersionTask()).start();  
  116.         }else{  
  117.             new Thread(new CopyDBTask()).start();  
  118.         }  
  119.           
  120.         AlphaAnimation aa  = new AlphaAnimation(0.1f, 1.0f);  
  121.         aa.setDuration(5000);  
  122.         RelativeLayout rl =  (RelativeLayout) this.findViewById(R.id.rl_splash);  
  123.         rl.startAnimation(aa);  
  124.     }  
  125.   
  126.       
  127.     //初始化完address.db数据库,不存在的拷贝任务   
  128.     private class CopyDBTask implements Runnable{  
  129.   
  130.         public void run() {  
  131.             try {  
  132.                 sendShowTextMessage();  
  133.                 addressService.copyDataBase();  
  134.                 sendHideTextMessage();  
  135.                 new Thread(new CheckVersionTask()).start();  
  136.             } catch (Exception e) {  
  137.                 e.printStackTrace();  
  138.             }  
  139.         }  
  140.         //向handle发消息,更新textview,显示正在下载   
  141.         private void sendShowTextMessage() {  
  142.             Message msg = new Message();  
  143.             msg.what = SHOW_MESSAGE;  
  144.             handler.sendMessage(msg);  
  145.         }  
  146.         //下载完后更新textview为不可见   
  147.         private void sendHideTextMessage() {  
  148.             Message msg = new Message();  
  149.             msg.what = HIDE_MESSAGE;  
  150.             handler.sendMessage(msg);  
  151.         }  
  152.     }  
  153.     //开辟线程检查是否需要更新升级 (解析服务器xml,判断服务器版本号与本地版本确定是否需要升级)   
  154.     private class CheckVersionTask implements Runnable {  
  155.         public void run() {  
  156.             String path = getApplicationContext().getResources().getString(  
  157.                     R.string.server_url);  
  158.             try {  
  159.                 URL url = new URL(path);  
  160.                 HttpURLConnection conn = (HttpURLConnection) url  
  161.                         .openConnection();  
  162.                 conn.setConnectTimeout(5000);  
  163.                 Thread.sleep(4000);  
  164.                 InputStream ins = conn.getInputStream();  
  165.                 info = UpdateInfoParser.getUpdateInfo(ins);  
  166.                 String version = getApplicationContext().getResources()  
  167.                         .getString(R.string.version);  
  168.                 if (version.equals(info.getVersion())) {  
  169.                     Log.i(TAG, "版本相同,无需升级");  
  170.                     Intent intent = new Intent(SplashActivity.this,MainScreenActivity.class);  
  171.                     startActivity(intent);  
  172.                     finish();  
  173.                 } else {  
  174.                     Log.i(TAG, "版本不相同,需要升级");  
  175.                     sendUpdateMessage();  
  176.                 }  
  177.             } catch (Exception e) {  
  178.                 Message msg = new Message();  
  179.                 msg.what = CONNECT_SERVER_ERROR;  
  180.                 handler.sendMessage(msg);  
  181.                 e.printStackTrace();  
  182.             }  
  183.         }  
  184.   
  185.         private void sendUpdateMessage() {  
  186.             Message msg = new Message();  
  187.             msg.what = NEED_UPDATE;  
  188.             handler.sendMessage(msg);  
  189.               
  190.         }  
  191.   
  192.     }  
  • 1
  • 2
  • 3
  • 下一页

相关内容