客户端升级

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值