Android开发:启动应用的数据库初始化和版本更新
| [日期:2011-08-18] |
来源:Linux社区
作者:anskya520 |
[字体:大 中 小]
|
- public class SplashActivity extends Activity {
- private static final String TAG = "mainactivity";
- private static final int NEED_UPDATE = 1;
- private static final int CONNECT_SERVER_ERROR = 2;
- private static final int SHOW_MESSAGE=3;
- private static final int HIDE_MESSAGE=4;
-
- UpdateInfo info;
- AddressService addressService;
- TextView tv_copydb;
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what) {
- case NEED_UPDATE:
- showUpdateDialog();
- break;
- case CONNECT_SERVER_ERROR:
- showErrorDialog();
- break;
- case SHOW_MESSAGE:
- tv_copydb.setVisibility(View.VISIBLE);
- break;
- case HIDE_MESSAGE:
- tv_copydb.setVisibility(View.INVISIBLE);
- break;
- }
- }
- private void showErrorDialog() {
- AlertDialog.Builder builder = new Builder(SplashActivity.this);
- builder.setTitle("错误");
- builder.setMessage("连接服务器失败");
- builder.setPositiveButton(R.string.ok, new OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
-
-
- Intent intent = new Intent(SplashActivity.this,MainScreenActivity.class);
- startActivity(intent);
- finish();
- }
- });
- builder.create().show();
- }
-
-
-
- private void showUpdateDialog() {
- AlertDialog.Builder builder = new Builder(SplashActivity.this);
-
-
-
- builder.setTitle(R.string.please_update);
- builder.setMessage(info.getDescription());
- builder.setPositiveButton(R.string.ok, new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- Log.i(TAG, "调用下载方法");
- final ProgressDialog pd = new ProgressDialog(SplashActivity.this);
- pd.setMessage(getApplicationContext().getResources().getString(R.string.downing));
- pd.show();
- try {
- new Thread(){
- public void run() {
- File file;
- try {
- file = DownLoadService.getFile(info.getUrl());
- install(file);
- pd.dismiss();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }}.start();
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- private void install(File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(file), "application/vnd.Android.package-archive");
- finish();
- startActivity(intent);
- }
- });
- builder.setNegativeButton(R.string.cancle, new OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
- Log.i(TAG, "用户取消");
- Intent intent = new Intent(SplashActivity.this,MainScreenActivity.class);
- startActivity(intent);
- finish();
- }
- });
- AlertDialog dialog = builder.create();
- dialog.show();
- }
- };
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.splashactivity);
- tv_copydb = (TextView) findViewById(R.id.copydatabase);
-
- addressService = new AddressService(this);
- boolean isexist = addressService.isExist();
- if(isexist){
- new Thread(new CheckVersionTask()).start();
- }else{
- new Thread(new CopyDBTask()).start();
- }
-
- AlphaAnimation aa = new AlphaAnimation(0.1f, 1.0f);
- aa.setDuration(5000);
- RelativeLayout rl = (RelativeLayout) this.findViewById(R.id.rl_splash);
- rl.startAnimation(aa);
- }
-
-
-
- private class CopyDBTask implements Runnable{
-
- public void run() {
- try {
- sendShowTextMessage();
- addressService.copyDataBase();
- sendHideTextMessage();
- new Thread(new CheckVersionTask()).start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- private void sendShowTextMessage() {
- Message msg = new Message();
- msg.what = SHOW_MESSAGE;
- handler.sendMessage(msg);
- }
-
- private void sendHideTextMessage() {
- Message msg = new Message();
- msg.what = HIDE_MESSAGE;
- handler.sendMessage(msg);
- }
- }
-
- private class CheckVersionTask implements Runnable {
- public void run() {
- String path = getApplicationContext().getResources().getString(
- R.string.server_url);
- try {
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url
- .openConnection();
- conn.setConnectTimeout(5000);
- Thread.sleep(4000);
- InputStream ins = conn.getInputStream();
- info = UpdateInfoParser.getUpdateInfo(ins);
- String version = getApplicationContext().getResources()
- .getString(R.string.version);
- if (version.equals(info.getVersion())) {
- Log.i(TAG, "版本相同,无需升级");
- Intent intent = new Intent(SplashActivity.this,MainScreenActivity.class);
- startActivity(intent);
- finish();
- } else {
- Log.i(TAG, "版本不相同,需要升级");
- sendUpdateMessage();
- }
- } catch (Exception e) {
- Message msg = new Message();
- msg.what = CONNECT_SERVER_ERROR;
- handler.sendMessage(msg);
- e.printStackTrace();
- }
- }
-
- private void sendUpdateMessage() {
- Message msg = new Message();
- msg.what = NEED_UPDATE;
- handler.sendMessage(msg);
-
- }
-
- }
