簡體   English   中英

Xamarin.Forms彈出“新版本可用”

[英]Xamarin.Forms popup “New Version Available”

我正在研究Xamarin.forms Android Project,我正在搜索為用戶顯示彈出窗口:

新版本可用

當用戶嘗試打開應用程序並在Play商店中提供新的更新時。

我認為最簡單的方法是在您自己的服務器上安裝一個返回當前版本號的Web服務,不幸的是,您需要在商店中更新應用程序時更新此版本號。

在GitHub Gist帳戶中創建一個包含最新版本號的文本文件。

獲取原始URL

string url =“ https://gist.githubusercontent.com/YOUR_ACCOUNT_NAME/0df1fa45aa11753de0a85893448b22de/raw/UpdateInfo.txt ”;

private static async Task<string> GetLatestVersion(string URL)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(URL));
        request.ContentType = "application/json"; //i am using a json file 
        request.Method = "GET";
        request.Timeout = 20000;
        // Send the request to the server and wait for the response:
        try
        {
            using (WebResponse response = await request.GetResponseAsync())
            {
                // Get a stream representation of the HTTP web response:
                using (Stream stream = response.GetResponseStream())
                {                    
                    StreamReader reader = new StreamReader(stream);
                    return reader.ReadToEnd();
                }
            }
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }

這將返回您應用的最新版本。 並查看活動中的現有應用版本

var versionName = Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName;
        var currentVer = double.Parse(versionName);

但是,每當您在Play商店中更新應用程序時,您都必須更新此版本號。

private class GetVersionCode extends AsyncTask<Void, String, String> {
    @Override
    protected String doInBackground(Void... voids) {

        String newVersion = null;
        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + SplashActivity.this.getPackageName() + "&hl=it")
                    .timeout(30000)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .get()
                    .select("div[itemprop=softwareVersion]")
                    .first()
                    .ownText();
             return newVersion;
        } catch (Exception e) {
            return newVersion;
        }
    }

    @Override
    protected void onPostExecute(String onlineVersion) {
        super.onPostExecute(onlineVersion);
        String currentVersion = null;
        try {
            currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (onlineVersion != null && !onlineVersion.isEmpty()) {
            if (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {

                    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SplashActivity.this);
                    alertDialogBuilder.setTitle("Product Update");
                    alertDialogBuilder.setMessage("A new version is available. Would you like to Upgrade now? (Current: "+currentVersion+" Latest: "+onlineVersion+")");
                    alertDialogBuilder.setPositiveButton("ok",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+SplashActivity.this.getPackageName())));
                                }
                            });

                    alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialogBuilder.setCancelable(true);
                            finish();
                            loginUserCheck();
                        }
                    });

                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
             }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM