android应用实现自动检测版本升级
在我们APP的开发中,往往都会遇到版本的升级,因为不可能有任何一个应用做的完美无缺,所以版本升级对APP应用来说是不可缺少的一部分.像新浪微博等一些应用软件,三天两头提醒我升级.不过这样也很正常,就像android 升级一样,为了给用户提供更方便更人性化的操作.说下具体实现吧,不过我是参考别人的。不管对你们有没有帮助,总之对我有帮助啊,如果日后用到就直接copy了.哈哈,不扯了。
首先看一个文件manifest文件:
我们可以很清楚的看到versionCode和versionName,我们一般用versionCode来实现, 实现原理很简单:服务器端有个serverVersion,我们本地有个localVersion.服务器端serverVersion>localVersion,这个时候我们就需要进行升级版本.原理大致就是这样。
具体实现请看下面:
01 | package com.jj.upgrade; |
03 | import com.jj.Service.UpdateService; |
05 | import android.app.AlertDialog; |
06 | import android.app.Application; |
07 | import android.content.DialogInterface; |
08 | import android.content.Intent; |
09 | import android.content.pm.PackageInfo; |
10 | import android.content.pm.PackageManager.NameNotFoundException; |
18 | public class MyApplication extends Application { |
20 | public static int localVersion = 0 ; // 本地安装版本 |
22 | public static int serverVersion = 2 ; // 服务器版本 |
24 | public static String downloadDir = "jj/" ; // 安装目录 |
27 | public void onCreate() { |
30 | PackageInfo packageInfo = getApplicationContext() |
31 | .getPackageManager().getPackageInfo(getPackageName(), 0 ); |
32 | localVersion = packageInfo.versionCode; |
33 | } catch (NameNotFoundException e) { |
38 | * 在这里写一个方法用于请求获取服务器端的serverVersion. |
我们一般把全局的东西放到application里面.
01 | public class MainActivity extends Activity { |
02 | private MyApplication myApplication; |
05 | public void onCreate(Bundle savedInstanceState) { |
06 | super .onCreate(savedInstanceState); |
07 | setContentView(R.layout.main); |
15 | public void checkVersion() { |
16 | myApplication = (MyApplication) getApplication(); |
17 | if (myApplication.localVersion < myApplication.serverVersion) { |
20 | AlertDialog.Builder alert = new AlertDialog.Builder( this ); |
21 | alert.setTitle( "软件升级" ) |
22 | .setMessage( "发现新版本,建议立即更新使用." ) |
23 | .setPositiveButton( "更新" , |
24 | new DialogInterface.OnClickListener() { |
25 | public void onClick(DialogInterface dialog, |
27 | Intent updateIntent = new Intent( |
30 | updateIntent.putExtra( |
32 | getResources().getString( |
34 | startService(updateIntent); |
37 | .setNegativeButton( "取消" , |
38 | new DialogInterface.OnClickListener() { |
39 | public void onClick(DialogInterface dialog, |
44 | alert.create().show(); |
我们在运行应用的时候要checkVersion();进行检查版本是否要进行升级. 最主要的是UpdateService服务类
02 | public int onStartCommand(Intent intent, int flags, int startId) { |
04 | app_name = intent.getStringExtra( "app_name" ); |
06 | FileUtil.createFile(app_name); // 创建文件 |
08 | createNotification(); // 首次创建 |
10 | createThread(); // 线程下载 |
12 | return super .onStartCommand(intent, flags, startId); |
创建路径及文件,这里就不介绍了,不明白了下载源码看。
首先我们先 看createNotification().这个方法:
04 | RemoteViews contentView; |
05 | public void createNotification() { |
06 | notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
07 | notification = new Notification(); |
08 | notification.icon = R.drawable.ic_launcher; // 这个图标必须要设置,不然下面那个RemoteViews不起作用. |
10 | notification.tickerText = "开始下载" ; |
12 | // updateIntent = new Intent(this, MainActivity.class); |
13 | // pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0); |
15 | // // 这里面的参数是通知栏view显示的内容 |
16 | // notification.setLatestEventInfo(this, app_name, "下载:0%", |
19 | // notificationManager.notify(notification_id, notification); |
22 | * 在这里我们用自定的view来显示Notification |
24 | contentView = new RemoteViews(getPackageName(), |
25 | R.layout.notification_item); |
26 | contentView.setTextViewText(R.id.notificationTitle, "正在下载" ); |
27 | contentView.setTextViewText(R.id.notificationPercent, "0%" ); |
28 | contentView.setProgressBar(R.id.notificationProgress, 100 , 0 , false ); |
30 | notification.contentView = contentView; |
32 | updateIntent = new Intent( this , MainActivity. class ); |
33 | updateIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); |
34 | pendingIntent = PendingIntent.getActivity( this , 0 , updateIntent, 0 ); |
36 | notification.contentIntent = pendingIntent; |
38 | notificationManager.notify(notification_id, notification); |
上面实现的也不难理解.(主要是初始化Notification,用于提醒用户开始下载)接着我们要看createThread方法
04 | public void createThread() { |
08 | final Handler handler = new Handler() { |
10 | public void handleMessage(Message msg) { |
14 | Uri uri = Uri.fromFile(FileUtil.updateFile); |
15 | Intent intent = new Intent(Intent.ACTION_VIEW); |
16 | intent.setDataAndType(uri, |
17 | "application/vnd.android.package-archive" ); |
19 | pendingIntent = PendingIntent.getActivity( |
20 | UpdateService. this , 0 , intent, 0 ); |
22 | notification.setLatestEventInfo(UpdateService. this , |
23 | app_name, "下载成功,点击安装" , pendingIntent); |
25 | notificationManager.notify(notification_id, notification); |
30 | notification.setLatestEventInfo(UpdateService. this , |
31 | app_name, "下载失败" , pendingIntent); |
43 | final Message message = new Message(); |
45 | new Thread( new Runnable() { |
50 | long downloadSize = downloadUpdateFile(down_url, |
51 | FileUtil.updateFile.toString()); |
52 | if (downloadSize > 0 ) { |
54 | message.what = DOWN_OK; |
55 | handler.sendMessage(message); |
58 | } catch (Exception e) { |
60 | message.what = DOWN_ERROR; |
61 | handler.sendMessage(message); |
这个方法有点小多,不过我想大家都看的明白,我在这里简单说名一下:首先我们创建一个handler用于检测最后下载ok还是not ok. 下面我们开启了线程进行下载数据。 我们接着看downloadUpdateFile这个方法:
05 | * @throws MalformedURLException |
07 | public long downloadUpdateFile(String down_url, String file) |
09 | int down_step = 5 ; // 提示step |
10 | int totalSize; // 文件总大小 |
11 | int downloadCount = 0 ; // 已经下载好的大小 |
12 | int updateCount = 0 ; // 已经上传的文件大小 |
13 | InputStream inputStream; |
14 | OutputStream outputStream; |
16 | URL url = new URL(down_url); |
17 | HttpURLConnection httpURLConnection = (HttpURLConnection) url |
19 | httpURLConnection.setConnectTimeout(TIMEOUT); |
20 | httpURLConnection.setReadTimeout(TIMEOUT); |
22 | totalSize = httpURLConnection.getContentLength(); |
23 | if (httpURLConnection.getResponseCode() == 404 ) { |
24 | throw new Exception( "fail!" ); |
26 | inputStream = httpURLConnection.getInputStream(); |
27 | outputStream = new FileOutputStream(file, false ); // 文件存在则覆盖掉 |
28 | byte buffer[] = new byte [ 1024 ]; |
30 | while ((readsize = inputStream.read(buffer)) != - 1 ) { |
31 | outputStream.write(buffer, 0 , readsize); |
32 | downloadCount += readsize; // 时时获取下载到的大小 |
37 | || (downloadCount * 100 / totalSize - down_step) >= updateCount) { |
38 | updateCount += down_step; |
40 | // notification.setLatestEventInfo(this, "正在下载...", updateCount |
41 | // + "%" + "", pendingIntent); |
42 | contentView.setTextViewText(R.id.notificationPercent, |
44 | contentView.setProgressBar(R.id.notificationProgress, 100 , |
47 | notificationManager.notify(notification_id, notification); |
52 | if (httpURLConnection != null ) { |
53 | httpURLConnection.disconnect(); |
注释已经写的很详细,相信大家都看的明白,如果哪里有不足的地方,请留您吉言指出。
这里我用别的app代替了,简单省事,正常的话,你要对你的APP进行数字签名。然后才可以进行升级应用。
示意图:
提示有新版
开始升级
升级下载中
下载完毕,点击安装
|