Android中如何实现自动更新
发表于:2026-11-05 作者:千家信息网编辑
千家信息网最后更新 2026年11月05日,本篇内容介绍了"Android中如何实现自动更新"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!代码如
千家信息网最后更新 2026年11月05日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安全错误
数据库的锁怎样保障安全
互联网科技股
伟宏互联网科技
华服互联网科技怎么样
鸠鸠互联网科技
科技互联网技术
关于新基建和互联网科技
与互联网科技公司
礼当家互联网科技
宜安科技 互联网金融
银优互联网科技有限公司
互联网科技缩写
壕鑫互联网络科技
互联网科技公司做什么
顶级互联网科技成果有哪些
互联网校园科技培训
浙江互联网科技研学营
湖南一路畅行互联网科技
互联网保险科技创业
保山互联网科技靠谱吗
河南商务圈互联网科技有限公司
科技金融是不是互联网金融
广州宏创互联网科技
融势互联网科技
狮子座互联网科技培训
哈尔滨新华互联网科技学院
湖南诚度互联网科技有限公司
移动互联网年度科技创新
国大互联网教育科技有限公司工作
广西科技产业互联网是什么
前海云享互联网科技