5

我正在Xamarin.forms的Android項目,我尋找而去顯示彈出用戶:Xamarin.Forms彈出「新版本可用」

新版本可用

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

+2

你檢查曲棍球應用程序? –

+0

我讀到它..也許我會用它 –

+1

你爲什麼想做這個擺在首位?如果您發佈應用程序,商店是否會自動更新爲新版本? – SilentStorm

回答

7

我認爲最簡單的事情將有自己的服務器上的Web服務,它返回當前的版本號,不幸的是你需要更新此版本號的任何時間更新在商店的應用程序。

+0

THX,把我寧願找動態解決方案, –

1
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(); 
      } 
+0

你可以請添加更多的描述 –

+0

你只要把你的應用程序的新版本從Play商店中,並檢查它現在的版本大於應用程序版本,如果它更大,只顯示彈出 –

2

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

獲取的原始URL

字符串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商店的應用程序。

+0

好的解決方案。但我仍然喜歡動態解決方案 –

相關問題