App应用的通用功能-创新互联

    App中有很多通用的功能,如设置模块,有缓存、无图模式、版本更新等一些通用的功能,与大家分享一下其中的版本检查更新,在我们的App中能自动检查更新升级。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、虚拟主机、营销软件、网站建设、龙城网站维护、网站推广。

    首先我们要先获得我们应用当前版本,接着从服务器获得应用的最新版本,两个一比较如果最新版本高于当前版本就同升级更新。

   代码实现:

   获得当前版本:

public static int getCurrVersion(Context context){

PackageManager pm = context.getPackageManager();

try {

PackageInfo info = pm.getPackageInfo(context

.getPackageName(), 0);

return info.versionCode;

} catch (NameNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return 0;

}

   从服务器获得最新版本:

public static UpdataInfo getUpdataInfo(InputStream is) throws Exception{  

  XmlPullParser  parser = Xml.newPullParser();    

   parser.setInput(is, "utf-8");//设置解析的数据源       7.    int type = parser.getEventType();  

   UpdataInfo info = new UpdataInfo();//实体      9.    while(type != XmlPullParser.END_DOCUMENT ){  

       switch (type) {  

      case XmlPullParser.START_TAG:  

          if("version".equals(parser.getName())){  

               info.setVersion(parser.nextText()); //获取版本号                  }else if ("url".equals(parser.getName())){  

               info.setUrl(parser.nextText()); //获取要升级的APK文件                  }else if ("description".equals(parser.getName())){  

              info.setDescription(parser.nextText()); //获取该文件的信息      18.            }  

         break;  

      }  

       type = parser.next();  

  }  

   return info;  

}

从服务器下载apk:

1.public static File getFileFromServer(String path, ProgressDialog pd) throws Exception{  

  //如果相等的话表示当前的sdcard挂载在手机上并且是可用的    

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  

      URL url = new URL(path);  

      HttpURLConnection conn =  (HttpURLConnection) url.openConnection();  

      conn.setConnectTimeout(5000);  

      //获取到文件的大小    

      pd.setMax(conn.getContentLength());  

      InputStream is = conn.getInputStream();  

      File file = new File(Environment.getExternalStorageDirectory(), "updata.apk");  

      FileOutputStream fos = new FileOutputStream(file);  

      BufferedInputStream bis = new BufferedInputStream(is);  

      byte[] buffer = new byte[1024];  

      int len ;  

      int total=0;  

      while((len =bis.read(buffer))!=-1){  

          fos.write(buffer, 0, len);  

          total+= len;  

          //获取当前下载量    

          pd.setProgress(total);  

      }  

.        fos.close();  

      bis.close();  

      is.close();  

      return file;  

  }  

  else{  

.        return null;  

  }  

然后进行安装

protected void installApk(File file) {  

  Intent intent = new Intent();  

  //执行动作  

  intent.setAction(Intent.ACTION_VIEW);  

  //执行的数据类型  

  intent.setDataAndType(Uri.fromFile(file), "application/vnd.Android.package-archive");//    

  startActivity(intent);  

}  



创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。

标题名称:App应用的通用功能-创新互联
网页链接:https://www.cdcxhl.com/article6/csipog.html

成都网站建设公司_创新互联,为您提供网页设计公司网站策划静态网站电子商务网站制作手机网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设