package t1.com;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import org.apache.http.client.HttpClient;import android.R.anim;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.app.ProgressDialog;import android.content.DialogInterface;import android.content.Intent;import android.content.SharedPreferences;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.widget.EditText;import android.widget.ListView;import android.widget.Toast;public class T1Activity extends Activity {    protected static final int UPDATA_CLIENT = 0;protected static final int CHECK_UPDATE = 1;protected static final int DOWN_ERROR = 0;/** Called when the activity is first created. */    private int globalVersion = 3;    private int serverVersion;    private String serverVersionUrl = "http://cloud.broadlink.com.cn/2/switch_version.html";    private String downLoadApkUrl = "http://cloud.broadlink.com.cn/2/t1.apk";        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);         ////检测更新-------------------------       new Thread(new Runnable() {             public void run() {             try {serverVersion = 33;                    Message msg = new Message();                      msg.what = CHECK_UPDATE;                      handler.sendMessage(msg);  } catch (NumberFormatException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}                                         }        }).start();                     }                 public void showUpdateDialog() {    @SuppressWarnings("unused")    AlertDialog alert = new AlertDialog.Builder(T1Activity.this).setTitle("更新提示")    .setIcon(android.R.drawable.ic_dialog_info).setMessage("检测到新的版本,请下载更新").setPositiveButton("下载",new DialogInterface.OnClickListener() {    public void onClick(DialogInterface dialog,int which) {    dialog.dismiss();  //关闭对话框      downLoadApk();     System.out.println("start update");    }    }).show();    }        protected void downLoadApk() {          final ProgressDialog pd;    //进度条对话框          pd = new  ProgressDialog(this);          pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);          pd.setMessage("正在下载更新");          pd.show();          new Thread(){              @Override              public void run() {                  try {                      File file = getFileFromServer(downLoadApkUrl, pd);                      sleep(3000);                      installApk(file);                      pd.dismiss(); //结束掉进度条对话框                  } catch (Exception e) {                  pd.dismiss();                    Message msg = new Message();                      msg.what = DOWN_ERROR;                      handler.sendMessage(msg);                      e.printStackTrace();                  }              }}.start();      }      public static File getFileFromServer(String path, ProgressDialog pd) throws Exception{          //如果相等的话表示当前的sdcard挂载在手机上并且是可用的          if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){              URL url = new URL(path);              HttpURLConnection conn =  (HttpURLConnection) url.openConnection();              conn.setConnectTimeout(5000);              //获取到文件的大小               pd.setMax(conn.getContentLength());              InputStream is = conn.getInputStream();              File file = new File(Environment.getExternalStorageDirectory(), "updata.apk");              FileOutputStream fos = new FileOutputStream(file);              BufferedInputStream bis = new BufferedInputStream(is);              byte[] buffer = new byte[1024];              int len ;              int total=0;              while((len =bis.read(buffer))!=-1){                  fos.write(buffer, 0, len);                  total+= len;                  //获取当前下载量                  pd.setProgress(total);              }              fos.close();              bis.close();              is.close();              return file;          }          else{              return null;          }      }   //安装apk       protected void installApk(File file) {          Intent intent = new Intent();          //执行动作          intent.setAction(Intent.ACTION_VIEW);          //执行的数据类型          intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");          startActivity(intent);      }         Handler handler = new Handler(){                  @Override          public void handleMessage(Message msg) {              // TODO Auto-generated method stub              super.handleMessage(msg);              switch (msg.what) {               case DOWN_ERROR:                  //下载apk失败                  Toast.makeText(getApplicationContext(), "下载新版本失败", 1).show();                  break;                case CHECK_UPDATE:                  //下载apk失败                                               if(serverVersion == 0) {                serverVersion = globalVersion;                }                                if(serverVersion != globalVersion){                showUpdateDialog();                }                 break;             }          }      };     }

更多相关文章

  1. android进度条对话框
  2. android定制对话框
  3. ch07 Android 日期与时间对话框
  4. 不让屏幕翻转扰乱你的进度对话框
  5. 从零开始--系统深入学习android(实践-让我们开始写代码-Android框
  6. 对话框式activity

随机推荐

  1. Android开发前景已成末路
  2. Android(安卓)Nine Patch图片及按钮背景
  3. Android从服务端获取json解析显示在客户
  4. 在 Android(安卓)设备上搭建 Web 服务器
  5. Android(安卓)touch mode和focusableInTo
  6. 【Android】第5章(1) 常用基本控件--本章示
  7. Android中子线程真的不能更新UI吗?
  8. Android系统架构图及简单的系统架构介绍
  9. Intent的设想与天马行空
  10. VirtualBox上编译android遇到共享文件夹