android 自动检测版本升级

标签: android 版本 升级 | 发表时间:2012-09-06 00:28 | 作者:jj120522
出处:http://blog.csdn.net

在我们APP的开发中,往往都会遇到版本的升级,因为不可能有任何一个应用做的完美无缺,所以版本升级对APP应用来说是不可缺少的一部分.像新浪微博等一些应用软件,三天两头提醒我升级.不过这样也很正常,就像android 升级一样,为了给用户提供更方便更人性化的操作.说下具体实现吧,不过我是参考别人的。不管对你们有没有帮助,总之对我有帮助啊,如果日后用到就直接copy了.哈哈,不扯了。

首先看一个文件manifest文件.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jj.upgrade"
    android:versionCode="1"
    android:versionName="1.0" >
我们可以很清楚的看到versionCode和versionName,我们一般用versionCode来实现,

实现原理很简单:服务器端有个serverVersion,我们本地有个localVersion.服务器端serverVersion>localVersion,这个时候我们就需要进行升级版本.原理大致就是这样。具体实现请看下面.

package com.jj.upgrade;

import com.jj.Service.UpdateService;

import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

/***
 * MyApplication
 * 
 * @author zhangjia
 * 
 */
public class MyApplication extends Application {

	public static int localVersion = 0;// 本地安装版本

	public static int serverVersion = 2;// 服务器版本

	public static String downloadDir = "jj/";// 安装目录

	@Override
	public void onCreate() {
		super.onCreate();
		try {
			PackageInfo packageInfo = getApplicationContext()
					.getPackageManager().getPackageInfo(getPackageName(), 0);
			localVersion = packageInfo.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		/***
		 * 在这里写一个方法用于请求获取服务器端的serverVersion.
		 */

	}

}

我们一般把全局的东西放到application里面.

public class MainActivity extends Activity {
	private MyApplication myApplication;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		checkVersion();
	}

	/***
	 * 检查是否更新版本
	 */
	public void checkVersion() {
		myApplication = (MyApplication) getApplication();
		if (myApplication.localVersion < myApplication.serverVersion) {

			// 发现新版本,提示用户更新
			AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle("软件升级")
					.setMessage("发现新版本,建议立即更新使用.")
					.setPositiveButton("更新",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Intent updateIntent = new Intent(
											MainActivity.this,
											UpdateService.class);
									updateIntent.putExtra(
											"app_name",
											getResources().getString(
													R.string.app_name));
									startService(updateIntent);
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							});
			alert.create().show();

		}
	}
}
我们在运行应用的时候要checkVersion();进行检查版本是否要进行升级.

最主要的是UpdateService服务类,

@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		app_name = intent.getStringExtra("app_name");
		// 创建文件
		FileUtil.createFile(app_name);// 创建文件

		createNotification();// 首次创建

		createThread();// 线程下载

		return super.onStartCommand(intent, flags, startId);

	}

创建路径及文件,这里就不介绍了,不明白了下载源码看.

首先我们先 看createNotification().这个方法:

/***
	 * 创建通知栏
	 */
	RemoteViews contentView;
	public void createNotification() {
		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		notification = new Notification();
		notification.icon = R.drawable.ic_launcher;// 这个图标必须要设置,不然下面那个RemoteViews不起作用.
		// 这个参数是通知提示闪出来的值.
		notification.tickerText = "开始下载";
		//
		// updateIntent = new Intent(this, MainActivity.class);
		// pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0);
		//
		// // 这里面的参数是通知栏view显示的内容
		// notification.setLatestEventInfo(this, app_name, "下载:0%",
		// pendingIntent);
		//
		// notificationManager.notify(notification_id, notification);

		/***
		 * 在这里我们用自定的view来显示Notification
		 */
		contentView = new RemoteViews(getPackageName(),
				R.layout.notification_item);
		contentView.setTextViewText(R.id.notificationTitle, "正在下载");
		contentView.setTextViewText(R.id.notificationPercent, "0%");
		contentView.setProgressBar(R.id.notificationProgress, 100, 0, false);

		notification.contentView = contentView;

		updateIntent = new Intent(this, MainActivity.class);
		updateIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0);

		notification.contentIntent = pendingIntent;

		notificationManager.notify(notification_id, notification);

	}
上面实现的也不难理解.(主要是初始化Notification,用于提醒用户开始下载)

接着我们要看createThread方法

/***
	 * 开线程下载
	 */
	public void createThread() {
		/***
		 * 更新UI
		 */
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case DOWN_OK:
					// 下载完成,点击安装
					Uri uri = Uri.fromFile(FileUtil.updateFile);
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setDataAndType(uri,
							"application/vnd.android.package-archive");

					pendingIntent = PendingIntent.getActivity(
							UpdateService.this, 0, intent, 0);

					notification.setLatestEventInfo(UpdateService.this,
							app_name, "下载成功,点击安装", pendingIntent);

					notificationManager.notify(notification_id, notification);

					stopSelf();
					break;
				case DOWN_ERROR:
					notification.setLatestEventInfo(UpdateService.this,
							app_name, "下载失败", pendingIntent);
					break;

				default:
					stopSelf();
					break;
				}

			}

		};

		final Message message = new Message();

		new Thread(new Runnable() {
			@Override
			public void run() {

				try {
					long downloadSize = downloadUpdateFile(down_url,
							FileUtil.updateFile.toString());
					if (downloadSize > 0) {
						// 下载成功
						message.what = DOWN_OK;
						handler.sendMessage(message);
					}

				} catch (Exception e) {
					e.printStackTrace();
					message.what = DOWN_ERROR;
					handler.sendMessage(message);
				}

			}
		}).start();
	}
这个方法有点小多,不过我想大家都看的明白,我在这里简单说名一下:首先我们创建一个handler用于检测最后下载ok还是not ok.

下面我们开启了线程进行下载数据。

我们接着看downloadUpdateFile这个方法:

/***
	 * 下载文件
	 * 
	 * @return
	 * @throws MalformedURLException
	 */
	public long downloadUpdateFile(String down_url, String file)
			throws Exception {
		int down_step = 5;// 提示step
		int totalSize;// 文件总大小
		int downloadCount = 0;// 已经下载好的大小
		int updateCount = 0;// 已经上传的文件大小
		InputStream inputStream;
		OutputStream outputStream;

		URL url = new URL(down_url);
		HttpURLConnection httpURLConnection = (HttpURLConnection) url
				.openConnection();
		httpURLConnection.setConnectTimeout(TIMEOUT);
		httpURLConnection.setReadTimeout(TIMEOUT);
		// 获取下载文件的size
		totalSize = httpURLConnection.getContentLength();
		if (httpURLConnection.getResponseCode() == 404) {
			throw new Exception("fail!");
		}
		inputStream = httpURLConnection.getInputStream();
		outputStream = new FileOutputStream(file, false);// 文件存在则覆盖掉
		byte buffer[] = new byte[1024];
		int readsize = 0;
		while ((readsize = inputStream.read(buffer)) != -1) {
			outputStream.write(buffer, 0, readsize);
			downloadCount += readsize;// 时时获取下载到的大小
			/**
			 * 每次增张5%
			 */
			if (updateCount == 0
					|| (downloadCount * 100 / totalSize - down_step) >= updateCount) {
				updateCount += down_step;
				// 改变通知栏
				// notification.setLatestEventInfo(this, "正在下载...", updateCount
				// + "%" + "", pendingIntent);
				contentView.setTextViewText(R.id.notificationPercent,
						updateCount + "%");
				contentView.setProgressBar(R.id.notificationProgress, 100,
						updateCount, false);
				// show_view
				notificationManager.notify(notification_id, notification);

			}

		}
		if (httpURLConnection != null) {
			httpURLConnection.disconnect();
		}
		inputStream.close();
		outputStream.close();

		return downloadCount;

	}
注释已经写的很详细,相信大家都看的明白,如果哪里有不足的地方,请留您吉言指出.

 这里我用别的app代替了,简单省事,正常的话,你要对你的APP进行数字签名.然后才可以进行升级应用. 

示意图:

                       

          提示有新版                                       开始升级                                        升级下载中                                  下载完毕,点击安装

 


    该睡觉了.








作者:jj120522 发表于2012-9-6 0:28:15 原文链接
阅读:10 评论:1 查看评论

相关 [android 版本 升级] 推荐:

android 自动检测版本升级

- - CSDN博客推荐文章
在我们APP的开发中,往往都会遇到版本的升级,因为不可能有任何一个应用做的完美无缺,所以版本升级对APP应用来说是不可缺少的一部分.像新浪微博等一些应用软件,三天两头提醒我升级.不过这样也很正常,就像android 升级一样,为了给用户提供更方便更人性化的操作.说下具体实现吧,不过我是参考别人的. 不管对你们有没有帮助,总之对我有帮助啊,如果日后用到就直接copy了.哈哈,不扯了.

罗技Revue升级Android 3.1版本 号称更好用

- 洞箫 - cnBeta.COM
喜欢逛电子商店的人们最近发现罗技的首款Google TV机顶盒产品包装发生变动,在左上角贴上了一个更新提示的贴纸,罗技表示新版Logitech Revue升级到了Android 3.1版本,并且引入了Android Market,提供了对第三方程序的支持,当然这也会带来第三方内容平台的大量内容.

Android 设备糟糕的版本升级史

- NOir - 爱范儿 · Beats of Bits
Google 宣布 Nexus One 将不会得到最新的 ICS 系统,这可能使许多人心情郁闷,但是在 the understatement 网站的 Micheal Degusta 看来,Nexus One 用户其实已经幸运很多. 他研究了 Android 手机的升级历史,并画出了一副图. 7/18 的 Android 手机在上市之后从未运行过最新的系统版本.

浅谈Android数据库版本升级及数据的迁移

- - CSDN博客推荐文章
Android开发涉及到的数据库采用的是轻量级的SQLite3,而在实际开发中,在存储一些简单的数据,使用SharedPreferences就足够了,只有在存储数据结构稍微复杂的时候,才会使用数据库来存储. 而数据库表的设计往往不是一开始就非常完美,可能在应用版本开发迭代中,表的结构也需要调整,这时候就涉及到数据库升级的问题了.

Android SQLite数据库版本升级原理解析

- - CSDN博客推荐文章
Android使用SQLite数据库保存数据,那数据库版本升级是怎么回事呢,这里说一下. 安装v1.0,假设v1.0版本只有一个account表,这时走继承SQLiteOpenHelper的onCreate,不走onUpgrade. 1、v1.0(直接安装v1.0). 1、v1.0   -->  v2.0              不走onCreate,走onUpgrade.

Chromium 升级到 V17 版本

- 龍渊冭子 - cnBeta.COM
尽管Chrome和Chromium的版本号没有任何意义,但我们还是不得不提一句,Chromium已经升级到了V17版本. 另外,Chrome Beta分支for Windows/Mac OS X/Linux和Chrome Frame则升级到了15.0.874.100,主要新特性:.

Android数据库升级实例

- - BlogJava-qileilove
  Andoird的SQLiteOpenHelper类中有一个onUpgrade方法. 经过实践,解决了我一连串的疑问:. 帮助文档里说的“数据库升级”是指什么.   你开发了一个程序,当前是1.0版本. 到1.1版本时,你在数据库的某个表中增加了一个字段. 那么软件1.0版本用的数据库在软件1.1版本就要被升级了.

Android 增量更新和升级

- - CSDN博客推荐文章
在年初的时候,尝试了一把热修复技术,当时选择的是阿里的andfix,使用起来也很简单,这里就不在多少,如果你对andfix有兴趣请链接: 点击打开链接. 虽然网上将热修复的文章很多,不过我还是想说原理,然后配合代码,我想这样大家理解更加深刻. 其实就是用ClassLoader加载机制,覆盖掉有问题的方法.

Android 升级适配爬坑历程 (mp.weixin.qq.com)

- - IT瘾-jianshu
code小生一个专注大前端领域的技术平台公众号回复 Android加入安卓技术群. 链接:https://www.jianshu.com/p/20252fc5c836. 声明:本文已获 如果执着授权发表,转发等请联系原作者授权. 以下部分适配是针对的是混合开发的项目,使用的是mui及h5+ api和原生代码实现.

Chrome Beta 升级到 15.0.874.100,Chromium 升级到 V17 版本

- Felix - 谷奥——探寻谷歌的奥秘
感谢读者 旧糖罐、chaos 和 小P孩儿 的提醒. 尽管Chrome和Chromium的版本号没有任何意义,但我们还是不得不提一句,Chromium已经升级到了V17版本. 另外,Chrome Beta分支for Windows/Mac OS X/Linux和Chrome Frame则升级到了15.0.874.100,主要新特性:.