您的位置:首页 > 移动开发

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

2015-06-04 08:03 489 查看
(本文讲解了在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的内容对应,这个很简单:
package com.example.appupdatedemo;  public class UpdateInfo {         private String version;         private String description;         private String url;                  public String getVersion()         {                 return version;         }         public void setVersion(String version)         {                 this.version = version;         }         public String getDescription()         {                 return description;         }         public void setDescription(String description)         {                 this.description = description;         }         public String getUrl()         {                 return url;         }         public void setUrl(String url)         {                 this.url = url;         }          }


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

package com.example.appupdatedemo;  import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;  import android.content.Context;  public class UpdateInfoService { 	public UpdateInfoService(Context context) { 	}  	public UpdateInfo getUpDateInfo() throws Exception { 		String path = GetServerUrl.getUrl() + "/update.txt"; 		StringBuffer sb = new StringBuffer(); 		String line = null; 		BufferedReader reader = null; 		try { 			// 创建一个url对象 			URL url = new URL(path); 			// 通過url对象,创建一个HttpURLConnection对象(连接) 			HttpURLConnection urlConnection = (HttpURLConnection) url 					.openConnection(); 			// 通过HttpURLConnection对象,得到InputStream 			reader = new BufferedReader(new InputStreamReader( 					urlConnection.getInputStream())); 			// 使用io流读取文件 			while ((line = reader.readLine()) != null) { 				sb.append(line); 			} 		} catch (Exception e) { 			e.printStackTrace(); 		} finally { 			try { 				if (reader != null) { 					reader.close(); 				} 			} catch (Exception e) { 				e.printStackTrace(); 			} 		} 		String info = sb.toString(); 		UpdateInfo updateInfo = new UpdateInfo(); 		updateInfo.setVersion(info.split("&")[1]); 		updateInfo.setDescription(info.split("&")[2]); 		updateInfo.setUrl(info.split("&")[3]); 		return updateInfo; 	}  }

这里获取文件的方法是先创建一个HttpURLConnection,再获取输入流。细心的朋友可能注意到其中有个类,叫GetServerUrl,这个类是用来存放后台地址信息的:
package com.example.appupdatedemo;  /**  * 获取服务器IP地址  */  public class GetServerUrl{ 	static String url="http://192.168.1.100:8080/PersonalHomePage";   //没错,我这里用的是本地的JAVAEE工程,各位根据实际情况修改。 			 	public static String getUrl() { 		return url; 	} }

OK,到了这一步,准备工作都做完了,接下来只剩一个类了!即我们的MainActicity,一共一百多行,我们分几部分来讲。
第一部分代码,做的工作是获取版本更新信息。
public class MainActivity extends Activity {  	// 更新版本要用到的一些信息 	private UpdateInfo info; 	private ProgressDialog pBar;  	@Override 	protected void onCreate(Bundle savedInstanceState) { 		super.onCreate(savedInstanceState); 		setContentView(R.layout.activity_main);  		Toast.makeText(MainActivity.this, "正在检查版本更新..", Toast.LENGTH_SHORT).show(); 		// 自动检查有没有新版本 如果有新版本就提示更新 		new Thread() { 			public void run() { 				try { 					UpdateInfoService updateInfoService = new UpdateInfoService( 							MainActivity.this); 					info = updateInfoService.getUpDateInfo(); 					handler1.sendEmptyMessage(0); 				} catch (Exception e) { 					e.printStackTrace(); 				} 			}; 		}.start(); 	}  	@SuppressLint("HandlerLeak") 	private Handler handler1 = new Handler() { 		public void handleMessage(Message msg) { 			// 如果有更新就提示 			if (isNeedUpdate()) {   //在下面的代码段 				showUpdateDialog();  //下面的代码段 			} 		}; 	};

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

第二部分,判断是否是最新版本,如果不是,跳出对话框选择是否更新:
private void showUpdateDialog() { 		AlertDialog.Builder builder = new AlertDialog.Builder(this); 		builder.setIcon(android.R.drawable.ic_dialog_info); 		builder.setTitle("请升级APP至版本" + info.getVersion()); 		builder.setMessage(info.getDescription()); 		builder.setCancelable(false);  		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  			@Override 			public void onClick(DialogInterface dialog, int which) { 				if (Environment.getExternalStorageState().equals( 						Environment.MEDIA_MOUNTED)) { 					downFile(info.getUrl());     //在下面的代码段 				} else { 					Toast.makeText(MainActivity.this, "SD卡不可用,请插入SD卡", 							Toast.LENGTH_SHORT).show(); 				} 			} 		}); 		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  			@Override 			public void onClick(DialogInterface dialog, int which) { 			}  		}); 		builder.create().show(); 	}  	private boolean isNeedUpdate() { 		 		String v = info.getVersion(); // 最新版本的版本号 		Log.i("update",v); 		Toast.makeText(MainActivity.this, v, Toast.LENGTH_SHORT).show(); 		if (v.equals(getVersion())) { 			return false; 		} else { 			return true; 		} 	}  	// 获取当前版本的版本号 	private String getVersion() { 		try { 			PackageManager packageManager = getPackageManager(); 			PackageInfo packageInfo = packageManager.getPackageInfo( 					getPackageName(), 0); 			return packageInfo.versionName; 		} catch (NameNotFoundException e) { 			e.printStackTrace(); 			return "版本号未知"; 		} 	}
这段里面要注意的是怎么获取当前版本,方法是使用PackageManager提供的getPackageInfo方法,返回的是manifest文件中的版本号。其他的代码挺简单,注释也挺全的。如果有问题,欢迎留言。
接下来是最后一部分,下载文件。
void downFile(final String url) {  		pBar = new ProgressDialog(MainActivity.this);    //进度条,在下载的时候实时更新进度,提高用户友好度 		pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 		pBar.setTitle("正在下载"); 		pBar.setMessage("请稍候..."); 		pBar.setProgress(0); 		pBar.show(); 		new Thread() { 			public void run() {         				HttpClient client = new DefaultHttpClient(); 				HttpGet get = new HttpGet(url); 				HttpResponse response; 				try { 					response = client.execute(get); 					HttpEntity entity = response.getEntity(); 					int length = (int) entity.getContentLength();   //获取文件大小                                         pBar.setMax(length);                            //设置进度条的总长度 					InputStream is = entity.getContent(); 					FileOutputStream fileOutputStream = null; 					if (is != null) { 						File file = new File( 								Environment.getExternalStorageDirectory(), 								"Test.apk"); 						fileOutputStream = new FileOutputStream(file); 						byte[] buf = new byte[10];   //这个是缓冲区,即一次读取10个比特,我弄的小了点,因为在本地,所以数值太大一 下就下载完了,看不出progressbar的效果。 						int ch = -1; 						int process = 0; 						while ((ch = is.read(buf)) != -1) {        							fileOutputStream.write(buf, 0, ch); 							process += ch; 							pBar.setProgress(process);       //这里就是关键的实时更新进度了! 						}  					} 					fileOutputStream.flush(); 					if (fileOutputStream != null) { 						fileOutputStream.close(); 					} 					down(); 				} catch (ClientProtocolException e) { 					e.printStackTrace(); 				} catch (IOException e) { 					e.printStackTrace(); 				} 			}  		}.start(); 	}  	void down() { 		handler1.post(new Runnable() { 			public void run() { 				pBar.cancel(); 				update(); 			} 		}); 	} //安装文件,一般固定写法 	void update() {                     		Intent intent = new Intent(Intent.ACTION_VIEW); 		intent.setDataAndType(Uri.fromFile(new File(Environment 				.getExternalStorageDirectory(), "Test.apk")), 				"application/vnd.android.package-archive"); 		startActivity(intent); 	}
这一段主要是利用progressdialog在下载的时候实时更新进度,主要利用的是一个字节数组的缓冲区。即每次获取到的内容填满缓冲区后就写入到本地本件中。这里我把缓冲区的大小设置为10个字节(1024会比较好),理由是因为在同一个局域网中速度特别快,刷一下就下载完了,看不出进度条效果,缓冲区调小点就OK了。
======================================== 写在后面: 源代码已上传到我的Github,或者到CSDN下载区下载。 任何问题,欢迎留言交流!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息