2016-07-07 2 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

관련 문제