android应用客户端自动升级 .

 
  1. import java.io.File; 

  2. import java.io.FileOutputStream;

  3. import java.io.IOException; 

  4. import java.io.InputStream; 

  5. import org.apache.http.HttpEntity;  
  6. import org.apache.http.HttpResponse;  
  7. import org.apache.http.client.ClientProtocolException;  
  8. import org.apache.http.client.HttpClient;  
  9. import org.apache.http.client.methods.HttpGet;  
  10. import org.apache.http.impl.client.DefaultHttpClient;  
  11.   
  12. import android.app.AlertDialog;  
  13. import android.app.Dialog;  
  14. import android.app.ProgressDialog;  
  15. import android.content.DialogInterface;  
  16. import android.content.Intent;  
  17. import android.net.Uri;  
  18. import android.os.Bundle;  
  19. import android.os.Environment;  
  20. import android.os.Handler;  
  21.   
  22. public class Update extends BaseActivity {  
  23.     public ProgressDialog pBar;  
  24.     private Handler handler = new Handler();  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.update);  
  30.         Dialog dialog = new AlertDialog.Builder(Update.this).setTitle("系统更新")  
  31.                 .setMessage("发现新版本,请更新!")// 设置内容  
  32.                 .setPositiveButton("确定",// 设置确定按钮  
  33.                         new DialogInterface.OnClickListener() {  
  34.   
  35.                             @Override  
  36.                             public void onClick(DialogInterface dialog,  
  37.                                     int which) {  
  38.                                 pBar = new ProgressDialog(Update.this);  
  39.                                 pBar.setTitle("正在下载");  
  40.                                 pBar.setMessage("请稍候...");  
  41.                                 pBar  
  42.                                         .setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  43.                                 downFile("http://url:8765/OA.apk");  
  44.                                   
  45.   
  46.                             }  
  47.   
  48.                         }).setNegativeButton("取消",   
  49.                         new DialogInterface.OnClickListener() {  
  50.                             public void onClick(DialogInterface dialog,  
  51.                                     int whichButton) {  
  52.                                 // 点击"取消"按钮之后退出程序  
  53.                                   
  54.                             }  
  55.                         }).create();// 创建  
  56.         // 显示对话框  
  57.         dialog.show();  
  58.   
  59.     }  
  60.   
  61.     void downFile(final String url) {  
  62.         pBar.show();  
  63.         new Thread() {  
  64.             public void run() {  
  65.                 HttpClient client = new DefaultHttpClient();  
  66.                 // params[0]代表连接的url  
  67.                 HttpGet get = new HttpGet(url);  
  68.                 HttpResponse response;  
  69.                 try {  
  70.                     response = client.execute(get);  
  71.                     HttpEntity entity = response.getEntity();  
  72.                     long length = entity.getContentLength();  
  73.                     InputStream is = entity.getContent();  
  74.                     FileOutputStream fileOutputStream = null;  
  75.                     if (is != null) {  
  76.   
  77.                         File file = new File(Environment  
  78.                                 .getExternalStorageDirectory(), "OA.apk");  
  79.                         fileOutputStream = new FileOutputStream(file);  
  80.                           
  81.                         byte[] buf = new byte[1024];  
  82.                         int ch = -1;  
  83.                         int count = 0;  
  84.                         while ((ch = is.read(buf)) != -1) {  
  85.                             // baos.write(buf, 0, ch);  
  86.                             fileOutputStream.write(buf, 0, ch);  
  87.                             count += ch;  
  88.                             if (length > 0) {  
  89.                               
  90.                             }  
  91.   
  92.                         }  
  93.   
  94.                     }  
  95.                     fileOutputStream.flush();  
  96.                     if (fileOutputStream != null) {  
  97.                         fileOutputStream.close();  
  98.                     }  
  99.                     down();  
  100.                 } catch (ClientProtocolException e) {  
  101.                     // TODO Auto-generated catch block  
  102.                     e.printStackTrace();  
  103.                 } catch (IOException e) {  
  104.                     // TODO Auto-generated catch block  
  105.                     e.printStackTrace();  
  106.                 }  
  107.             }  
  108.   
  109.         }.start();  
  110.   
  111.     }  
  112.   
  113.     void down() {  
  114.         handler.post(new Runnable() {  
  115.             public void run() {  
  116.                 pBar.cancel();  
  117.                 update();  
  118.             }  
  119.         });  
  120.   
  121.     }  
  122.   
  123.     void update() {  
  124.   
  125.         Intent intent = new Intent(Intent.ACTION_VIEW);  
  126.         intent.setDataAndType(Uri.fromFile(new File("/sdcard/OA.apk")),  
  127.                 "application/vnd.android.package-archive");  
  128.         startActivity(intent);  
  129.     }  
  130.   

你可能感兴趣的:(android应用客户端自动升级 .)