App应用的通用功能

发布时间:2020-06-24 05:57:50 作者:androidlinyk
来源:网络 阅读:523

     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);    

}    



推荐阅读:
  1. Q语言 我的命令库《通用》窗口操作之绑定应用界面功能函数
  2. 关于APP上传应用市场

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

服务器 return 通用

上一篇:shell编程之case分支语句

下一篇:string直接分成char数组

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》