代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
自动检查更新APK
作者:
癫狂俗人
/ 发布于
2014/10/14
/
695
package com.tutor.update; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.Toast; public class MainAcitivity extends Activity { int NewVerCode; int localVercode; private JSONObject jsonObject = null; private UpdateManager mUpdateManager; private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: checkVersion(); break; default: break; } }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getRemoteVersion(); } protected void checkVersion() { try { localVercode = getPackageManager().getPackageInfo( "com.tutor.update", 0).versionCode; // 这里来检测版本是否需要更新 if (NewVerCode > localVercode) { mUpdateManager = new UpdateManager(MainAcitivity.this); String downloadUrl = /* jsonObject.getString("downloadUrl") */ "http://www.chongchi-tech.com/ccsoft/uploads/app/SmartHome1.0.16_201410091748.apk"; String verComment = jsonObject.getString("verComment"); mUpdateManager.checkUpdateInfo(downloadUrl, verComment); } else { Toast.makeText(this, "已是最新版本无需更新!", 0).show(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void getRemoteVersion() { new Thread() { public void run() { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = 0; URL url; try { url = new URL( "http://www.chongchi-tech.com/ccsoft/uploads/app/updateInfo.txt"); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); InputStream inStream = conn.getInputStream(); while ((len = inStream.read(data)) != -1) { outStream.write(data, 0, len); } inStream.close(); String result = new String(outStream.toByteArray(), "GBK");// 通过out.Stream.toByteArray获取到写的数据 jsonObject = new JSONObject(result); NewVerCode = jsonObject.getInt("verCode"); Message msg = new Message(); msg.what = 1; mHandler.sendMessage(msg); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); } } package com.tutor.update; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.apache.http.HttpResponse; import android.app.AlertDialog; import android.app.Dialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnClickListener; import android.net.Uri; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.widget.ProgressBar; import android.widget.Toast; public class UpdateManager { private Context mContext; // 提示语 private String updateMsg = ""; // 返回的安装包url private String apkUrl = ""; private Dialog noticeDialog; private Dialog downloadDialog; /* 下载包安装路径 */ private static final String savePath = "/sdcard/updatedemo/"; private static final String saveFileName = savePath + "UpdateDemoRelease.apk"; /* 进度条与通知ui刷新的handler和msg常量 */ private ProgressBar mProgress; private static final int DOWN_UPDATE = 1; private static final int DOWN_OVER = 2; private int progress; private Thread downLoadThread; private boolean interceptFlag = false; private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case DOWN_UPDATE: mProgress.setProgress(progress); break; case DOWN_OVER: downloadDialog.cancel(); installApk(); break; default: break; } }; }; public UpdateManager(Context context) { this.mContext = context; } // 外部接口让主Activity调用 public void checkUpdateInfo(String apkUrl, String updateMsg) { this.apkUrl = apkUrl; this.updateMsg = updateMsg; showNoticeDialog(); } private void showNoticeDialog() { AlertDialog.Builder builder = new Builder(mContext); builder.setTitle("软件版本更新"); builder.setMessage(updateMsg); builder.setPositiveButton("下载", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); showDownloadDialog(); } }); builder.setNegativeButton("以后再说", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); noticeDialog = builder.create(); noticeDialog.show(); } private void showDownloadDialog() { AlertDialog.Builder builder = new Builder(mContext); builder.setTitle("软件版本更新"); final LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.progress, null); mProgress = (ProgressBar) v.findViewById(R.id.progress); builder.setView(v); builder.setCancelable(false); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); interceptFlag = true; } }); downloadDialog = builder.create(); downloadDialog.show(); downloadApk(); } private Runnable mdownApkRunnable = new Runnable() { @Override public void run() { try { URL url = new URL(apkUrl); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.connect(); int length = conn.getContentLength(); InputStream is = conn.getInputStream(); File file = new File(savePath); if (!file.exists()) { file.mkdir(); } String apkFile = saveFileName; File ApkFile = new File(apkFile); FileOutputStream fos = new FileOutputStream(ApkFile); int count = 0; byte buf[] = new byte[1024]; do { int numread = is.read(buf); count += numread; progress = (int) (((float) count / length) * 100); // 更新进度 mHandler.sendEmptyMessage(DOWN_UPDATE); if (numread <= 0) { // 下载完成通知安装 mHandler.sendEmptyMessage(DOWN_OVER); break; } fos.write(buf, 0, numread); } while (!interceptFlag);// 点击取消就停止下载. fos.close(); is.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; /** * 下载apk * * @param url */ private void downloadApk() { downLoadThread = new Thread(mdownApkRunnable); downLoadThread.start(); } /** * 安装apk * * @param url */ private void installApk() { File apkfile = new File(saveFileName); if (!apkfile.exists()) { return; } Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive"); mContext.startActivity(i); } }
试试其它关键字
自动更新
检查更新
APK
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
癫狂俗人
贡献的其它代码
(
7
)
.
获取IP地址
.
自动检查更新APK
.
获取某网站的服务器时间
.
二维数组的遍历
.
创建guid
.
Oracle 日期分页
.
Oracle存储过程分离表中的数据
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3