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 block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.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;
            } 
        } 
    };
   
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()