Android:检查更新是否可用

时间:2013-06-29 19:03:15

标签: android

我正在开发一个检查更新是否可用的应用程序。问题是我的应用程序无法识别自己是否在最新版本中。

我的服务器上有一个文件,该文件包含最新版本号(1.0.1)。我的应用程序版本也是1.0.1但是当我检查更新时,我的应用程序表明有更新可用,文本说最新版本和安装版本是1.0.1。

所以这是我的代码:

(checkupdatebutton)
{
    setContentView(R.layout.updater);


    DefaultHttpClient httpclient = new DefaultHttpClient();

    try {
        HttpGet httppost = new HttpGet("http://myserver.com/latestversion.txt");
        HttpResponse response;
        response = httpclient.execute(httppost);
        HttpEntity ht = response.getEntity();
        BufferedHttpEntity buf = new BufferedHttpEntity(ht);
        InputStream is = buf.getContent();
        BufferedReader r = new BufferedReader(new InputStreamReader(is,"UTF-8"));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line + "\n");
            }
            TextView versionupdnew = (TextView)findViewById(R.id.versionupdnew);
            //Set text to value of my file (latestversion.txt)
            versionupdnew.setText(total);

            TextView installedversion = (TextView)findViewById(R.id.versionupdcur);
            installedversion.setText(R.string.version);

            TextView title = (TextView)findViewById(R.id.title);
            if(installedversion.equals(versionupdnew))
            {
            //Current version is latest
        title.setText("No update needed!");
            Button buttonUpdate = (Button)findViewById(R.id.buttonUpdate);
            buttonUpdate.setEnabled(false);
            }else{
              title.setText("New version is available!");
            }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
    }
}

3 个答案:

答案 0 :(得分:2)

当两个框的文本值相等时,

TextView.equals()不会返回true。您需要比较两个框的文本值。

例如:

if(installedversion.getText().toString().equals(versionupdnew.getText().toString()))

请注意TextView.getText()函数不返回StringAPI documentation个状态,它返回CharSequence,但如果设置为使用Spannable }或Editable它可以转换为其他类型。在这些类型上使用toString()会将框的文本值作为String返回,根据我的经验,这是最可靠的比较方式两个View s)的值

答案 1 :(得分:1)

使用SharedPreferences存储最新版本,否则使用R.string.version将始终检查已安装apk附带的版本,并且在下载新版本时,它不会更新此版本。

如果将版本映射到整数,这将是最简单的,这样您就不需要解析版本​​字符串。每个新版本都比最后一个版本大1。

  1. 获取当前版本:

    // use preferences to get the current version, with default = 1 
    // (replace 1 with the version that came with the apk)
    preferences.getInt("current_version", 1); 
    
  2. 从您的服务器获取版本。

  3. 检查服务器版本是否大于当前版本,并在必要时进行更新:

    if (server_version > current_version) {
       update();
       preferences_editor.putInt("current_version", server_version).commit();
    }
    
  4. 现在,当您下次检查是否需要更新时,您将检查最近更新的版本。

答案 2 :(得分:0)

对于您当前的问题,您正在比较TextViews而不是字符串,因此比较字符串将有所帮助。

虽然我会建议你采用其他方法,

- 您可以在服务器上设置一个小型API,说“check_version”

- 您将当前版本号传递给API,例如:“www.myServer.com/check_version.php?v = 1.0.1”

- API将返回一些JSON说

{
   status:"UPDATE_AVAILABLE",
   message: "An update is available",
   url: "http://play.google.com?xxxyyyzzz"
}

在这里,您可以使用“状态”字段来确定更新是否可用..它还可以通知更新是否至关重要..就像您可以拥有可能的值

  • “UPDATE_AVAILABLE”

  • “UPDATE_REQUIRED”

  • “最新”。

    如果消息和URL的“最新”,则该消息和URL可以为空。所有比较逻辑都将在服务器上。

相关问题