android 新版本下载 安装

本文详细介绍了应用程序如何实现自动检查并下载新版本的过程。通过服务器返回版本信息,并与当前版本号进行对比,若存在更新,则下载新版本并完成安装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 首先在程序启动的时候 向服务器发送请求 服务器会返回    String  newVerString="2|app.quannaojiaoyu.com......"

2 然后根据 “|”拆分为俩个list 元素   取第一个list 元素 和当前的版本号进行比较 如果此元素大于版本号 则发现新版本开始下载 url 为list的第二个元素  下载完毕即可安装
   String newVerString = functions.getHttpResponse(
     urlApi + "base.asp", "getVer");
   System.out.println("newString--->"+newVerString);
   Log.d("StartPage", "Check Server.");
   if (newVerString != null)
   {
    ArrayList<String> listVerStrings = functions.splitString(newVerString);
         //得到当前版本号

    int curVersion = functions.getAppVersionName(getApplicationContext());

              
    Log.d("StartPage", listVerStrings.get(0));
    // Check New Version
    if (curVersion < Integer.parseInt(listVerStrings.get(0)))
    {
     Log.d("StartPage", "find new version.");

     try
     {
    
      URL Url = new URL(listVerStrings.get(1));
      URLConnection conn = Url.openConnection();
      conn.connect();
      InputStream is = conn.getInputStream();
      int fileSize = conn.getContentLength();//获取文件长度

      Log.d("StartPage", "new version download start ("
        + String.valueOf(fileSize) + ")...");

      msg = new Message();
      msg.what = UPDATE_NEWVER;
      msg.arg1 = fileSize;
      handler.sendMessage(msg);

      if (fileSize <= 0)return;
      FileOutputStream FOS = new FileOutputStream(strSDPath
        + "/cawords.apk");
      byte buf[] = new byte[1024];
      int downLoadFilePosition = 0;
      int numread;
      while ((numread = is.read(buf)) != -1)
      {
       FOS.write(buf, 0, numread);
       downLoadFilePosition += numread;
       Log.d("StartPage", "new version downloading ("
         + String.valueOf(downLoadFilePosition)
         + ")...");
      
       msg = new Message();
       msg.what = UPDATE_DOWNLOAD;
       msg.arg1 = downLoadFilePosition;
       handler.sendMessage(msg);
      }

      Log.d("StartPage", "New Version Downloaded.");

      Intent intent = new Intent();
      // 安装程序
      intent.setDataAndType(
        Uri.fromFile(new File(strSDPath
          + "/com.quannaojiaoyu.cadict.apk")),
        "application/vnd.android.package-archive");
      startActivity(intent);
      Log.d("StartPage", "New Version Installed.");
      finish();
      System.exit(0);

     } catch (Exception ex)
     {
    
    如何得到当前版本号:

public static int getAppVersionName(Context context)
{
  int versionName = -1;
  try
  {
   PackageManager pm = context.getPackageManager();
   PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
   versionName = pi.versionCode;
   Log.d("versionName ", versionName +"");
  } catch (Exception e)
  {
   Log.e("GET VER", "Exception", e);
  }
  return versionName;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值