2016-07-07 187 views
0

我试图让我的应用程序的版本名称,以及保存在名为ver.txt的SD卡上的.txt文件中的版本名称。我可以轻松获取我的应用程序版本,但是如何读取存储在我的.txt文件中的版本?如何从SD卡读取原始文本文件?

// get local apk version code 
PackageManager manager = context.getPackageManager(); 
PackageInfo info = null; 
try { 
    info = manager.getPackageInfo(context.getPackageName(), 0); 
} catch (NameNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

// get version code from downloaded .txt file 
// 

// compareVersions 
if (info.versionCode >= 1) { 
    Toast.makeText(context, "up to date!", Toast.LENGTH_LONG).show(); 
} else { 
    Toast.makeText(context, "updates are available", Toast.LENGTH_LONG).show(); 
} 
+1

您无法将文件作为常规文本文件读取吗? (http://stackoverflow.com/a/12421888/3741176) –

+0

这是文件的内容?只是版本号? –

+0

是的先生,只是数字的数字,没有字母,它读取“101” – MaggotSauceYumYum

回答

0

这不是完整的解决方案,但你明白了。

int updateVersionCode = info.versionCode; 

try { 
    File sdcard = Environment.getExternalStorageDirectory(); 
    File file = new File(sdcard, "version.txt"); 

    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line = null; 

    if((line = br.readLine()) != null){ 
     updateVersionCode = Integer.parseInt(line); 
    } 
    br.close(); 
}catch (Exception e) { 
    //Handle errors! 
    e.printStackTrace(); 
} 


if (info.versionCode < updateVersionCode) { 
    //The local version is minor, so updates are avaliable... 
} 
+0

if(info.versionCode> = 1){'其中'info.versionCode'是本地apk版本,'1'将会被它从.txt文件。希望这是有道理的 – MaggotSauceYumYum

+0

我不明白为什么int updateVersionCode = info.versionCode;? – MaggotSauceYumYum

+0

他们为什么平等? – MaggotSauceYumYum