首页 > 代码库 > android的apk自动检测升级
android的apk自动检测升级
首先获取本地apk版本:
/**
* 获取本地软件版本
*/
public static int getLocalVersion(Context ctx){
int localVersion = 0;
try {
PackageInfo packageInfo = ctx.getApplicationContext()
.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
localVersion = packageInfo.versionCode;
Log.d("TAG", "本软件的版本。。"+localVersion);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return localVersion;
}
然后获取服务器版本,这个可以通过很多方式获取就自己发挥
获取服务器版本后跟本地apk版本进行比较:
如果低于服务器版本就进行更新:(本人写的仅供参考,我是异步下载的,然后通知的方式显示进度)
/** * 用于更新app版本 * * @param ctx * 上下文对象 * @param url * 更新版本的地址 */ private static PendingIntent pendingIntent; public static void UpdateVersion(final Context ctx, String url) { createNotification(ctx); // 创建文件,读取app_name createFile(ctx.getResources().getString(R.string.app_name)); new AsyncTask<String, Void, String>() { @Override protected String doInBackground(String... params) { int down_step = 1;// 提示step int totalSize;// 文件总大小 int downloadCount = 0;// 已经下载好的大小 int updateCount = 0;// 已经上传的文件大小 InputStream inputStream; OutputStream outputStream; URL url; HttpURLConnection httpURLConnection = null; try { url = new URL(params[0]); httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setConnectTimeout(TIMEOUT); httpURLConnection.setReadTimeout(TIMEOUT); // 获取下载文件的size totalSize = httpURLConnection.getContentLength(); // Log.d("TAG", "totalSize"+totalSize); if (httpURLConnection.getResponseCode() == 404) { throw new Exception("fail!"); } inputStream = httpURLConnection.getInputStream(); // File appFile=File.createTempFile("中拓钢铁",".apk"); outputStream = new FileOutputStream(updateFile, false);// 文件存在则覆盖掉 byte buffer[] = new byte[1024]; int readsize = 0; while ((readsize = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, readsize); downloadCount += readsize;// 时时获取下载到的大小 // Log.d("TAG", "downloadCount"+downloadCount); /** * 每次增张1% */ if (updateCount == 0 || (downloadCount * 100 / totalSize - down_step) >= updateCount) { updateCount += down_step; // 改变通知栏 // Log.d("TAG", "开始下载。。"); builder = new Notification.Builder(ctx) .setSmallIcon(R.drawable.logo) .setContentText( "正在下载(" + updateCount + "%)...") .setProgress(100, updateCount, false); manager.notify(8, builder.build()); } } if (httpURLConnection != null) { httpURLConnection.disconnect(); } inputStream.close(); outputStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { // 下载完成,点击安装 System.out.println("updateFile.." + updateFile); Uri uri = Uri.fromFile(updateFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.android.package-archive"); pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0); builder = new Notification.Builder(ctx) .setSmallIcon(R.drawable.logo) .setContentIntent(pendingIntent) .setContentTitle("apk下载更新") .setContentText("下载成功,请点击安装") .setProgress(100, 100, false); manager.notify(8, builder.build()); } }.execute(url); } private static void createNotification(Context ctx) { // 最普通的通知栏 manager = (NotificationManager) ctx .getSystemService(ctx.NOTIFICATION_SERVICE); builder = new Notification.Builder(ctx).setSmallIcon(R.drawable.logo) .setContentTitle("apk下载更新").setContentText("准备下载...") .setProgress(100, 0, false); manager.notify(8, builder.build()); }
当下载完成后,可以点击安装。
注:当你的apk是签名后的,如果手机上面的apk是没有签名的,那么在安装时会提示签名不同的包冲突,无法安装,这样只能卸载没有签名的apk后再安装
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。