安卓应用的版本更新

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.updateApp_demo"
 android:versionCode="1" android:versionName="1.0" >
如上:安卓配置文件中,有版本号与版本名,一般用版本号(整型)来比较版本的新旧。下面是在代码中获取版本:
context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;//或者用.versionName获取版本名

新版本下载的部分主要代码:
ProgressDialog m_progressDlg = new ProgressDialog(this);
m_progressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
m_progressDlg.setIndeterminate(false);
m_progressDlg.setTitle("正在下载");
m_progressDlg.setMessage("请稍候...");
m_progressDlg.show();
......
</pre><pre code_snippet_id="1597264" snippet_file_name="blog_20160304_5_2502288" name="code" class="java">//下面的代码在线程中进行
......
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
 HttpResponse response = client.execute(get);
 HttpEntity entity = response.getEntity();
 long length = entity.getContentLength();

 m_progressDlg.setMax((int)length);//设置进度条的最大值

 InputStream is = entity.getContent();
 FileOutputStream fileOutputStream = null;
 if (is != null) {
  File file = new File(Environment.getExternalStorageDirectory(),m_appNameStr);
  fileOutputStream = new FileOutputStream(file);
  byte[] buf = new byte[1024];
  int ch = -1;
  int count = 0;
  while ((ch = is.read(buf)) != -1) {
   fileOutputStream.write(buf, 0, ch);
   count += ch;
   if (length > 0) {
    m_progressDlg.setProgress(count);
   }
  }
 }
 fileOutputStream.flush();
 if (fileOutputStream != null) {
 fileOutputStream.close();
 }
 m_mainHandler.post(new Runnable() {
  public void run() {
   m_progressDlg.cancel();
   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setDataAndType(Uri.fromFile(
     new File(Environment.getExternalStorageDirectory(), m_appName)),
     "application/vnd.android.package-archive");
     startActivity(intent);
  }
 });
 } catch (ClientProtocolException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
}

也可以使用一些平台的推送功能更新(如极光推送):http://docs.jpush.io/guideline/android_guide/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值