Example #1
0
        public static Verison GetLatestVerison()
        {
            Verison verison = new Verison();

            try
            {
                string      RawVersionInfo            = httpRequest.GetGerResult("https://raw.githubusercontent.com/UMI64/UinfoWork/master/UinfoWork/Properties/AndroidManifest.xml");
                string      RawVersionDiscriptionInfo = httpRequest.GetGerResult(" https://raw.githubusercontent.com/UMI64/UinfoWork/master/UinfoWork/Resources/values/Strings.xml");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(RawVersionInfo);
                XmlNode rootNode = xmlDoc.SelectSingleNode("manifest");
                verison.VersionCode = rootNode.Attributes["android:versionCode"].Value;
                verison.VersionName = rootNode.Attributes["android:versionName"].Value;
                xmlDoc.LoadXml(RawVersionDiscriptionInfo);
                rootNode = xmlDoc.SelectSingleNode("resources");
                verison.VersionDiscription = string.Empty;
                foreach (XmlNode node in rootNode)
                {
                    if (node.Attributes["name"].Value == "Discription")
                    {
                        verison.VersionDiscription = node.InnerText;
                    }
                }
                verison.VersionDiscription = verison.VersionDiscription.Replace("\\", string.Empty);
            }
            catch
            {
            }
            return(verison);
        }
Example #2
0
        public static Verison GetLocalVersion(Context context)
        {
            Verison version = new Verison();
            // 获取packagemanager的实例
            PackageManager packageManager = context.PackageManager;
            // getPackageName()是你当前类的包名
            PackageInfo packInfo = packageManager.GetPackageInfo(context.PackageName, 0);

            version.VersionCode        = packInfo.VersionCode.ToString();
            version.VersionName        = packInfo.VersionName;
            version.VersionDiscription = context.GetString(Resource.String.Discription);
            return(version);
        }
Example #3
0
        public static Verison GetAPKVersion(string absPath, Context context)
        {
            Verison        version = new Verison();
            PackageManager pm      = context.PackageManager;
            PackageInfo    pkgInfo = pm.GetPackageArchiveInfo(absPath, PackageInfoFlags.Activities);

            if (pkgInfo != null)
            {
                ApplicationInfo appInfo = pkgInfo.ApplicationInfo;
                /* 必须加这两句,不然下面icon获取是default icon而不是应用包的icon */
                appInfo.SourceDir       = absPath;
                appInfo.PublicSourceDir = absPath;
                version.VersionCode     = pkgInfo.VersionCode.ToString(); // 得到版本信息 
                version.VersionName     = pkgInfo.VersionName;
            }
            else
            {
                version.VersionCode = "0";
                version.VersionName = "0";
            }
            version.VersionDiscription = string.Empty;
            return(version);
        }
Example #4
0
            protected override string RunInBackground(string[] @params)
            {
                Timer  aTimer = new Timer(1000);
                string Url    = @params[0];

                int[] OutPutDatas = new int[3];
                try
                {
                    if (!Directory.Exists(fileSavePath))
                    {
                        Directory.CreateDirectory(fileSavePath);
                    }
                    if (File.Exists(filePath))
                    {
                        Verison LocalVerison = Verison.GetLocalVersion(updataActivity);
                        Verison APKVerison   = Verison.GetAPKVersion(filePath, updataActivity);
                        if (LocalVerison < APKVerison)//版本小于则安装
                        {
                            return(Success);
                        }
                        else
                        {
                            File.Delete(filePath);
                        }
                        //大于等于则删除
                        //继续下载
                    }
                    System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url);

                    System.Net.WebResponse response = request.GetResponse();
                    Stream inputStream = response.GetResponseStream();
                    length = inputStream.Length;
                    byte[] buffer    = new byte[1024];
                    Stream outStream = File.Create(filePath);
                    Stream inStream  = response.GetResponseStream();
                    aTimer.Elapsed  += new ElapsedEventHandler(OnTimedEvent);
                    aTimer.AutoReset = true;
                    aTimer.Enabled   = true;
                    while (!IsCancel)
                    {
                        int ReadLength = 0;
                        ReadLength = inStream.Read(buffer, 0, buffer.Length);
                        if (ReadLength > 0)
                        {
                            outStream.Write(buffer, 0, ReadLength);
                        }
                        else
                        {
                            break;
                        }
                        count += ReadLength;
                        int Progress = (int)(((float)count / length) * 100);
                        // 更新进度条
                        OutPutDatas[0] = Progress;
                        OutPutDatas[1] = count;
                        OutPutDatas[2] = (int)length;
                        PublishProgress(OutPutDatas);
                    }
                    aTimer.Close();
                    outStream.Close();
                    inStream.Close();
                }
                catch (System.Exception e)
                {
                    return(e.Message);
                }
                if (IsCancel)
                {
                    return(Failure);
                }
                return(Success);
            }