0

我知道方法

ApplicationDescriptor.currentApplicationDescriptor();

但我的目标是下载另一个 JAD 并将其版本号与当前应用程序版本进行比较。最好/最简单的方法是什么?有没有办法从一个简单的字符串构造一个 ApplicationDescriptor ?

4

1 回答 1

0

从当前的 Apps JAD 获取版本:

String currentVersion = ApplicationDescriptor.currentApplicationDescriptor().getVersion();

从下载的 JAD 中获取版本作为字符串:

public static String getJADProperty(String jadString, String propKey) {
    int indexFrom = jadString.indexOf(propKey) + propKey.length();

    // Value reaches until line break (Unix vs. Win)
    int indexTo = jadString.indexOf("\r", indexFrom);
    if (indexTo == -1) { indexTo = jadString.indexOf("\n", indexFrom); }

    return jadString.substring(indexFrom, indexTo).trim();
}

比较版本字符串:

/**
 * Compares two version strings that are in the format 1.1.1
 * 
 * @param current   Version String in the format 1.1.1 (current version of the app)
 * @param remote    Version String in the format 1.1.1 (remote version of the app)
 * 
 * @return true if the remote version is newer
 */
public static boolean compareVersionStrings(String current, String remote) {
    int lastIndexCurrent = 0;
    int indexCurrent = 0;

    int lastIndexRemote = 0;
    int indexRemote = 0;

    String currentVersionSubstring = "";
    String remoteVersionSubstring = "";

    do {
        lastIndexCurrent = indexCurrent + currentVersionSubstring.length();
        indexCurrent = current.indexOf(".", lastIndexCurrent);
        lastIndexRemote = indexRemote + remoteVersionSubstring.length();
        indexRemote = remote.indexOf(".", lastIndexRemote);

        // Needed because there is no "." at the last number of the version string
        if (indexCurrent != -1) {
            currentVersionSubstring = current.substring(lastIndexCurrent, indexCurrent);
        } else {
            currentVersionSubstring = current.substring(lastIndexCurrent);
        }
        if (indexRemote != -1) {
            remoteVersionSubstring = remote.substring(lastIndexRemote, indexRemote);
        } else {
            remoteVersionSubstring = remote.substring(lastIndexRemote);
        }

        if (Integer.parseInt(currentVersionSubstring) < Integer.parseInt(remoteVersionSubstring)) {
            return true;
        }
    } while (indexCurrent != -1);

    // 1.0 < 1.0.1
    if (indexRemote != -1) {
        return true;
    }

    return false;
}

任何更正和改进表示赞赏。随时编辑并与我分享您的经验。

于 2011-11-27T02:04:41.483 回答