2011-11-26 95 views
0

我知道方法黑莓手機:如何在一個JAD閱讀並提取版本號

ApplicationDescriptor.currentApplicationDescriptor(); 

但我的目標是下載另一個JAD和比較它的版本號爲當前應用程序的版本。什麼是最好/最簡單的方法來做到這一點?有沒有辦法從一個簡單的字符串構造一個ApplicationDescriptor?

回答

0

獲取從目前的應用程序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; 
} 

任何更正和改進表示讚賞。隨意編輯並與我分享您的經驗。