原文地址:android 自动更新apk版本
截图如下:
代码实现如下:
package com.update.apk; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.util.Log; public class MainActivity extends Activity { /** Called when the activity is first created. */ String newVerName = "";//新版本名称 int newVerCode = -1;//新版本号 ProgressDialog pd = null; String UPDATE_SERVERAPK = "ApkUpdateAndroid.apk"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if(getServerVer()){ int verCode = this.getVerCode(this); if(newVerCode>verCode){ doNewVersionUpdate();//更新版本 }else{ notNewVersionUpdate();//提示已是最新版本 } } } /** * 获得版本号 */ public int getVerCode(Context context){ int verCode = -1; try { verCode = context.getPackageManager().getPackageInfo("com.update.apk", 0).versionCode; } catch (NameNotFoundException e) { // TODO Auto-generated catch block Log.e("版本号获取异常", e.getMessage()); } return verCode; } /** * 获得版本名称 */ public String getVerName(Context context){ String verName = ""; try { verName = context.getPackageManager().getPackageInfo("com.update.apk", 0).versionName; } catch (NameNotFoundException e) { Log.e("版本名称获取异常", e.getMessage()); } return verName; } /** * 从服务器端获得版本号与版本名称 * @return */ public boolean getServerVer(){ try { URL url = new URL("http://10.0.2.2:8080/ApkUpdateService/ver"); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setDoInput(true); httpConnection.setDoOutput(true); httpConnection.setRequestMethod("GET"); httpConnection.connect(); InputStreamReader reader = new InputStreamReader(httpConnection.getInputStream()); BufferedReader bReader = new BufferedReader(reader); String json = bReader.readLine(); JSONArray array = new JSONArray(json); JSONObject jsonObj = array.getJSONObject(0); newVerCode = Integer.parseInt(jsonObj.getString("verCode")); newVerName = jsonObj.getString("verName"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } /** * 不更新版本 */ public void notNewVersionUpdate(){ int verCode = this.getVerCode(this); String verName = this.getVerName(this); StringBuffer sb = new StringBuffer(); sb.append("当前版本:"); sb.append(verName); sb.append(" Code:"); sb.append(verCode); sb.append("\n已是最新版本,无需更新"); Dialog dialog = new AlertDialog.Builder(this) .setTitle("软件更新") .setMessage(sb.toString()) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub finish(); } }).create(); dialog.show(); } /** * 更新版本 */ public void doNewVersionUpdate(){ int verCode = this.getVerCode(this); String verName = this.getVerName(this); StringBuffer sb = new StringBuffer(); sb.append("当前版本:"); sb.append(verName); sb.append(" Code:"); sb.append(verCode); sb.append(",发现版本:"); sb.append(newVerName); sb.append(" Code:"); sb.append(verCode); sb.append(",是否更新"); Dialog dialog = new AlertDialog.Builder(this) .setTitle("软件更新") .setMessage(sb.toString()) .setPositiveButton("更新", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub pd = new ProgressDialog(MainActivity.this); pd.setTitle("正在下载"); pd.setMessage("请稍后。。。"); pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); downFile("http://10.0.2.2:8080/ApkUpdateService/ApkUpdateAndroid.apk"); } }) .setNegativeButton("暂不更新", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub finish(); } }).create(); //显示更新框 dialog.show(); } /** * 下载apk */ public void downFile(final String url){ pd.show(); new Thread(){ public void run(){ HttpClient client = new DefaultHttpClient(); 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(),UPDATE_SERVERAPK); fileOutputStream = new FileOutputStream(file); byte[] b = new byte[1024]; int charb = -1; int count = 0; while((charb = is.read(b))!=-1){ fileOutputStream.write(b, 0, charb); count += charb; } } fileOutputStream.flush(); if(fileOutputStream!=null){ fileOutputStream.close(); } down(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); pd.cancel(); update(); } }; /** * 下载完成,通过handler将下载对话框取消 */ public void down(){ new Thread(){ public void run(){ Message message = handler.obtainMessage(); handler.sendMessage(message); } }.start(); } /** * 安装应用 */ public void update(){ Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(),UPDATE_SERVERAPK)) , "application/vnd.android.package-archive"); startActivity(intent); } }
<!--end: topics 文章、评论容器-->
相关推荐
适配Android10以下及以上,下载更新apk自动安装,解决自动安装失败的问题。真实有用的,希望对大家有帮助。
Android apk发布到google play上检测google play的新版本,并且下载更新
android 自动安装apk的demo,可根据自己的需求进行修改。
Android 自动更新 下载文件 APK 源代码 实例
Android自动更新的demo 我们这里不考虑下载配置文件和解析配置文件的内容,只考虑第二种情况就是我们获取了更新的apk的网络地址,如何更新。
用于unity Android 平台做apk自动更新
android版本自动更新安装 替换旧的apk
这个demo实现了android开发应用内下载安装apk升级版本的功能,非常简便轻量集成,完全原生代码即可实现,无需第三方框架。。。
androidAPK自动更新亲测在android 10.0、9.0下的华为机子没问题,太穷了所以收取5积分,该项目里面也包含自动获取授权
Qt for Android实现自动更新app,安装apk文件,qml自动更新 通过调用修改Java函数来实现对私有数据的读写
android 快速放电工具,可用于电池相关的android开发人员。
unity 内部更新Apk 自动安装 支持安卓8.0 以上 demo.apk 测试包
Android实现下载APK文件并自动执行安装
- 服务端提供版本信息和apk下载地址 - 客户端通过对比版本进行文件下载安装升级 # 客户端 ## 权限清单(AndroidManifest.xml) ```xml <manifest xmlns:android=...
一. 打开项目选择如图示1 (build.gradle 项目... 自动追加版本号和版本名称 android.applicationVariants.all { variant->variant.outputs.each { output-> output.outputFile = new File(output.outputFile.paren
android 自动获取用户手机上所有apk的包名,可根据需求进行修改。
如果用过一些手机助手的朋友都知道,手机助手下载其他软件的更新程序后会自动静默更新。APK静默安装是指实现了在APK文件安装过程中不会出现系统安装对话框。不仅可以安装新的apk,也适用于更新旧的apk。更新apk必须...
实现 android7、8 以上版本自动更新app 程序,仅供参考。
Android8.0之前 未知应用安装权限默认开启,如下图所示 8.0以下手机未知应用安装权限.png Android8.0之后 未知应用安装权限默认关闭,且权限入口隐藏。 如何开启未知应用安装权限的入口,并设置允许安装? 在...
android 无sdcard 下载安装 apk 多线程 断点 自动安装apk