(本文讲解了在Android中实现APP版本更新,文末附有源码。)
创新互联自2013年起,先为太谷等服务建站,太谷等地企业,进行企业商务咨询服务。为太谷企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
2.与后台的交互
3.Android中Handler的使用
4.Android中ProgressDialog的使用
话不多说,先来看看效果图:
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; } }
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; } }
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; } }
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(); //下面的代码段 } }; };
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 "版本号未知"; } }
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下载区下载。
任何问题,欢迎留言交流!