Android 轻松实现后台搭建+APP版本更新

(本文讲解了在Android中实现APP版本更新,文末附有源码。)

看完本文,您可以学到:


1.版本更新的方法

2.与后台的交互

3.Android中Handler的使用

4.Android中ProgressDialog的使用

话不多说,先来看看效果图:





一、大致思路阐述


首先,我们要有一个可以被手机访问的后台。

这里有两种方法,在调试的时候我们可以利用手机和笔记本连到同一个局域网的方式,在电脑上开启个类似PHP或者JAVAEE一样样的后台服务。

但是对于没有相关后台开发经验的朋友,这里有一种更好的方式:利用Github等免费空间来实现。详细请戳我的另一篇博文利用Github建立你的个人网站 。

OK,有了存放资源的后台,我们要放点什么东西呢?很简单,一个包含最新版本信息的update.txt文件和一个.apk文件足矣!


txt文件里写啥?看下我的例子:

XXX&1.3&这里写点描述&http://192.168.1.100:8080/PersonalHomePage/new.apk

解释一下: &是分隔符,用于手机端获取到信息后的分割。1.3代表着最新版本号,之后的是新版本的描述,最后的是新版本APK的下载地址(这 里我用了局域网)。一开始的是啥呢?我当时在试验的时候,在开头并没有加额外信息,即以1.3开头,实验之后,发现手机端获取到TXT文本信息后不能正确 解析,原因我觉得是因为TXT文件的开头包含有一些自带的字符,手机解析时会有问题。(感兴趣的朋友可以去深究,还望不吝赐教!)


OK,有了新版本的信息,我们要怎么做?

我们要获取到最新的版本号,然后与当前APP的版本号进行对比。如果低于最新版本,就到下载地址中去下载。


二、详细代码解释


首先,新建一个UpdateInfo类,用来与update.txt的内容对应,这个很简单:


[java] view plaincopy

  1. package com.example.appupdatedemo;  

  2.   

  3. public class UpdateInfo  

  4. {  

  5.         private String version;  

  6.         private String description;  

  7.         private String url;  

  8.           

  9.         public String getVersion()  

  10.         {  

  11.                 return version;  

  12.         }  

  13.         public void setVersion(String version)  

  14.         {  

  15.                 this.version = version;  

  16.         }  

  17.         public String getDescription()  

  18.         {  

  19.                 return description;  

  20.         }  

  21.         public void setDescription(String description)  

  22.         {  

  23.                 this.description = description;  

  24.         }  

  25.         public String getUrl()  

  26.         {  

  27.                 return url;  

  28.         }  

  29.         public void setUrl(String url)  

  30.         {  

  31.                 this.url = url;  

  32.         }  

  33.           

  34. }  



然后,写一个类去获取更新的信息,即我们的update.txt文件:


UpdateInfoService:


[java] view plaincopy

  1. package com.example.appupdatedemo;  

  2.   

  3. import java.io.BufferedReader;  

  4. import java.io.InputStreamReader;  

  5. import java.net.HttpURLConnection;  

  6. import java.net.URL;  

  7.   

  8. import android.content.Context;  

  9.   

  10. public class UpdateInfoService {  

  11.     public UpdateInfoService(Context context) {  

  12.     }  

  13.   

  14.     public UpdateInfo getUpDateInfo() throws Exception {  

  15.         String path = GetServerUrl.getUrl() + "/update.txt";  

  16.         StringBuffer sb = new StringBuffer();  

  17.         String line = null;  

  18.         BufferedReader reader = null;  

  19.         try {  

  20.             // 创建一个url对象  

  21.             URL url = new URL(path);  

  22.             // 通过url对象,创建一个HttpURLConnection对象(连接)  

  23.             HttpURLConnection urlConnection = (HttpURLConnection) url  

  24.                     .openConnection();  

  25.             // 通过HttpURLConnection对象,得到InputStream  

  26.             reader = new BufferedReader(new InputStreamReader(  

  27.                     urlConnection.getInputStream()));  

  28.             // 使用io流读取文件  

  29.             while ((line = reader.readLine()) != null) {  

  30.                 sb.append(line);  

  31.             }  

  32.         } catch (Exception e) {  

  33.             e.printStackTrace();  

  34.         } finally {  

  35.             try {  

  36.                 if (reader != null) {  

  37.                     reader.close();  

  38.                 }  

  39.             } catch (Exception e) {  

  40.                 e.printStackTrace();  

  41.             }  

  42.         }  

  43.         String info = sb.toString();  

  44.         UpdateInfo updateInfo = new UpdateInfo();  

  45.         updateInfo.setVersion(info.split("&")[1]);  

  46.         updateInfo.setDescription(info.split("&")[2]);  

  47.         updateInfo.setUrl(info.split("&")[3]);  

  48.         return updateInfo;  

  49.     }  

  50.   

  51. }  


这里获取文件的方法是先创建一个HttpURLConnection,再获取输入流。细心的朋友可能注意到其中有个类,叫GetServerUrl,这个类是用来存放后台地址信息的:


[java] view plaincopy

  1. package com.example.appupdatedemo;  

  2.   

  3. /** 

  4.  * 获取服务器IP地址 

  5.  */  

  6.   

  7. public class GetServerUrl{  

  8.     static String url="http://192.168.1.100:8080/PersonalHomePage";   //没错,我这里用的是本地的JAVAEE工程,各位根据实际情况修改。  

  9.               

  10.     public static String getUrl() {  

  11.         return url;  

  12.     }  

  13. }  


OK,到了这一步,准备工作都做完了,接下来只剩一个类了!即我们的MainActicity,一共一百多行,我们分几部分来讲。


第一部分代码,做的工作是获取版本更新信息。


[java] view plaincopy

  1. public class MainActivity extends Activity {  

  2.   

  3.     // 更新版本要用到的一些信息  

  4.     private UpdateInfo info;  

  5.     private ProgressDialog pBar;  

  6.   

  7.     @Override  

  8.     protected void onCreate(Bundle savedInstanceState) {  

  9.         super.onCreate(savedInstanceState);  

  10.         setContentView(R.layout.activity_main);  

  11.   

  12.         Toast.makeText(MainActivity.this"正在检查版本更新..", Toast.LENGTH_SHORT).show();  

  13.         // 自动检查有没有新版本 如果有新版本就提示更新  

  14.         new Thread() {  

  15.             public void run() {  

  16.                 try {  

  17.                     UpdateInfoService updateInfoService = new UpdateInfoService(  

  18.                             MainActivity.this);  

  19.                     info = updateInfoService.getUpDateInfo();  

  20.                     handler1.sendEmptyMessage(0);  

  21.                 } catch (Exception e) {  

  22.                     e.printStackTrace();  

  23.                 }  

  24.             };  

  25.         }.start();  

  26.     }  

  27.   

  28.     @SuppressLint("HandlerLeak")  

  29.     private Handler handler1 = new Handler() {  

  30.         public void handleMessage(Message msg) {  

  31.             // 如果有更新就提示  

  32.             if (isNeedUpdate()) {   //在下面的代码段  

  33.                 showUpdateDialog();  //下面的代码段  

  34.             }  

  35.         };  

  36.     };  


这里我们用到了new Thread+ Handler的方式去进行异步加载版本信息,主要是因为在安卓中要把耗时任务放在非主线程中执行,否则会造成阻塞,抛出无响应异常。还有另外的实现方式是安卓封装的AsyncTask,具体可以参考这篇博文:Android AsyncTask详解



第二部分,判断是否是最新版本,如果不是,跳出对话框选择是否更新:


[java] view plaincopy

  1. private void showUpdateDialog() {  

  2.     AlertDialog.Builder builder = new AlertDialog.Builder(this);  

  3.     builder.setIcon(android.R.drawable.ic_dialog_info);  

  4.     builder.setTitle("请升级APP至版本" + info.getVersion());  

  5.     builder.setMessage(info.getDescription());  

  6.     builder.setCancelable(false);  

  7.   

  8.     builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  

  9.   

  10.         @Override  

  11.         public void onClick(DialogInterface dialog, int which) {  

  12.             if (Environment.getExternalStorageState().equals(  

  13.                     Environment.MEDIA_MOUNTED)) {  

  14.                 downFile(info.getUrl());     //在下面的代码段  

  15.             } else {  

  16.                 Toast.makeText(MainActivity.this"SD卡不可用,请插入SD卡",  

  17.                         Toast.LENGTH_SHORT).show();  

  18.             }  

  19.         }  

  20.     });  

  21.     builder.setNegativeButton("取消"new DialogInterface.OnClickListener() {  

  22.   

  23.         @Override  

  24.         public void onClick(DialogInterface dialog, int which) {  

  25.         }  

  26.   

  27.     });  

  28.     builder.create().show();  

  29. }  

  30.   

  31. private boolean isNeedUpdate() {  

  32.       

  33.     String v = info.getVersion(); // 最新版本的版本号  

  34.     Log.i("update",v);  

  35.     Toast.makeText(MainActivity.this, v, Toast.LENGTH_SHORT).show();  

  36.     if (v.equals(getVersion())) {  

  37.         return false;  

  38.     } else {  

  39.         return true;  

  40.     }  

  41. }  

  42.   

  43. // 获取当前版本的版本号  

  44. private String getVersion() {  

  45.     try {  

  46.         PackageManager packageManager = getPackageManager();  

  47.         PackageInfo packageInfo = packageManager.getPackageInfo(  

  48.                 getPackageName(), 0);  

  49.         return packageInfo.versionName;  

  50.     } catch (NameNotFoundException e) {  

  51.         e.printStackTrace();  

  52.         return "版本号未知";  

  53.     }  

  54. }  

这段里面要注意的是怎么获取当前版本,方法是使用PackageManager提供的getPackageInfo方法,返回的是manifest文件中的版本号。其他的代码挺简单,注释也挺全的。如果有问题,欢迎留言。


接下来是最后一部分,下载文件。


[java] view plaincopy

  1.     void downFile(final String url) {   

  2.         pBar = new ProgressDialog(MainActivity.this);    //进度条,在下载的时候实时更新进度,提高用户友好度  

  3.         pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  

  4.         pBar.setTitle("正在下载");  

  5.         pBar.setMessage("请稍候...");  

  6.         pBar.setProgress(0);  

  7.         pBar.show();  

  8.         new Thread() {  

  9.             public void run() {          

  10.                 HttpClient client = new DefaultHttpClient();  

  11.                 HttpGet get = new HttpGet(url);  

  12.                 HttpResponse response;  

  13.                 try {  

  14.                     response = client.execute(get);  

  15.                     HttpEntity entity = response.getEntity();  

  16.                     int length = (int) entity.getContentLength();   //获取文件大小  

  17.                                         pBar.setMax(length);                            //设置进度条的总长度  

  18.                     InputStream is = entity.getContent();  

  19.                     FileOutputStream fileOutputStream = null;  

  20.                     if (is != null) {  

  21.                         File file = new File(  

  22.                                 Environment.getExternalStorageDirectory(),  

  23.                                 "Test.apk");  

  24.                         fileOutputStream = new FileOutputStream(file);  

  25.                         byte[] buf = new byte[10];   //这个是缓冲区,即一次读取10个比特,我弄的小了点,因为在本地,所以数值太大一 下就下载完了,看不出progressbar的效果。  

  26.                         int ch = -1;  

  27.                         int process = 0;  

  28.                         while ((ch = is.read(buf)) != -1) {         

  29.                             fileOutputStream.write(buf, 0, ch);  

  30.                             process += ch;  

  31.                             pBar.setProgress(process);       //这里就是关键的实时更新进度了!  

  32.                         }  

  33.   

  34.                     }  

  35.                     fileOutputStream.flush();  

  36.                     if (fileOutputStream != null) {  

  37.                         fileOutputStream.close();  

  38.                     }  

  39.                     down();  

  40.                 } catch (ClientProtocolException e) {  

  41.                     e.printStackTrace();  

  42.                 } catch (IOException e) {  

  43.                     e.printStackTrace();  

  44.                 }  

  45.             }  

  46.   

  47.         }.start();  

  48.     }  

  49.   

  50.     void down() {  

  51.         handler1.post(new Runnable() {  

  52.             public void run() {  

  53.                 pBar.cancel();  

  54.                 update();  

  55.             }  

  56.         });  

  57.     }  

  58. //安装文件,一般固定写法  

  59.     void update() {                      

  60.         Intent intent = new Intent(Intent.ACTION_VIEW);  

  61.         intent.setDataAndType(Uri.fromFile(new File(Environment  

  62.                 .getExternalStorageDirectory(), "Test.apk")),  

  63.                 "application/vnd.android.package-archive");  

  64.         startActivity(intent);  

  65.     }  

这 一段主要是利用progressdialog在下载的时候实时更新进度,主要利用的是一个字节数组的缓冲区。即每次获取到的内容填满缓冲区后就写入到本地 本件中。这里我把缓冲区的大小设置为10个字节(1024会比较好),理由是因为在同一个局域网中速度特别快,刷一下就下载完了,看不出进度条效果,缓冲 区调小点就OK了。


========================================

写在后面:

源代码已上传到我的Github,或者到CSDN下载区下载。

任何问题,欢迎留言交流!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章