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后再安装






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值