千家信息网

Android中如何实现自动更新

发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,本篇内容介绍了"Android中如何实现自动更新"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!代码如
千家信息网最后更新 2025年11月13日Android中如何实现自动更新

本篇内容介绍了"Android中如何实现自动更新"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

代码如下:

import java.io.File;  import java.io.FileOutputStream;import java.io.IOException;  import java.io.InputStream;  import org.apache.http.HttpEntity;  import org.apache.http.HttpResponse;  import org.apache.http.client.ClientProtocolException;  import org.apache.http.client.HttpClient;  import org.apache.http.client.methods.HttpGet;  import org.apache.http.impl.client.DefaultHttpClient;    import android.app.AlertDialog;  import android.app.Dialog;  import android.app.ProgressDialog;  import android.content.DialogInterface;  import android.content.Intent;  import android.net.Uri;  import android.os.Bundle;  import android.os.Environment;  import android.os.Handler;    public class Update extends BaseActivity {      public ProgressDialog pBar;      private Handler handler = new Handler();        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.update);          Dialog dialog = new AlertDialog.Builder(Update.this).setTitle("系统更新")                  .setMessage("发现新版本,请更新!")// 设置内容                  .setPositiveButton("确定",// 设置确定按钮                          new DialogInterface.OnClickListener() {                                @Override                              public void onClick(DialogInterface dialog,                                      int which) {                                  pBar = new ProgressDialog(Update.this);                                  pBar.setTitle("正在下载");                                  pBar.setMessage("请稍候...");                                  pBar                                          .setProgressStyle(ProgressDialog.STYLE_SPINNER);                                  downFile("http://url:8765/OA.apk");                                                                  }                            }).setNegativeButton("取消",                            new DialogInterface.OnClickListener() {                              public void onClick(DialogInterface dialog,                                      int whichButton) {                                  // 点击"取消"按钮之后退出程序                                                                }                          }).create();// 创建          // 显示对话框          dialog.show();        }        void downFile(final String url) {          pBar.show();          new Thread() {              public void run() {                  HttpClient client = new DefaultHttpClient();                  // params[0]代表连接的url                  HttpGet get = new HttpGet(url);                  HttpResponse response;                  try {                      response = client.execute(get);                      HttpEntity entity = response.getEntity();                      long length = entity.getContentLength();                      InputStream is = entity.getContent();                      FileOutputStream fileOutputStream = null;                      if (is != null) {                            File file = new File(Environment                                  .getExternalStorageDirectory(), "OA.apk");                          fileOutputStream = new FileOutputStream(file);                                                    byte[] buf = new byte[1024];                          int ch = -1;                          int count = 0;                          while ((ch = is.read(buf)) != -1) {                              // baos.write(buf, 0, ch);                              fileOutputStream.write(buf, 0, ch);                              count += ch;                              if (length > 0) {                                                            }                            }                        }                      fileOutputStream.flush();                      if (fileOutputStream != null) {                          fileOutputStream.close();                      }                      down();                  } catch (ClientProtocolException e) {                      // TODO Auto-generated catch block                      e.printStackTrace();                  } catch (IOException e) {                      // TODO Auto-generated catch block                      e.printStackTrace();                  }              }            }.start();        }        void down() {          handler.post(new Runnable() {              public void run() {                  pBar.cancel();                  update();              }          });        }        void update() {            Intent intent = new Intent(Intent.ACTION_VIEW);          intent.setDataAndType(Uri.fromFile(new File("/sdcard/OA.apk")),                  "application/vnd.android.package-archive");          startActivity(intent);      }    }

"Android中如何实现自动更新"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

更新 内容 按钮 更多 知识 实用 学有所成 接下来 代码 代表 困境 实际 对话框 情况 文章 案例 正在 程序 系统 编带 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 安徽工控软件开发哪家实惠 网络安全人人有责重如泰山 国家网络安全学院分数线 徐州达内科技软件开发 数据库 慢 关于初中网络安全活动简报 网吧玩cod16连接不上服务器 三级网络技术最新题型 学校国家网络安全周活动计划 西瓜视频会员显示服务器异常 四川大学网络安全专业考研分数线 深圳自主可控软件开发报价表 数据库重置与备份 动态网站与数据库的关系 爱乐园互联网科技公司 双流区网络安全吗 docker 直播服务器 操作系统和数据库安全技术 河北大学网络安全大赛 商城软件开发哪家服务周到 影像数据导入数据库 学网络安全需要懂哪些 长春软件开发就找吉网传媒 滁州游戏服务器价格如何计算 渭南高新区网络安全大讲堂 数据库插入一条元组 专业版的软件开发合同 天行服务器换手机版下载 黄浦区优势软件开发服务密度 数据库做医院预约挂号系统
0