我正在尝试开发安卓应用;我需要检查是否有比当前版本更高的版本 -From play store-?
我试着写这段代码:
try {
URL updateURL = new URL("https://play.google.com/store/apps/details?id=com.XXXX.YYYY");
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
final String s = new String(baf.toByteArray());
/* Get current Version Number */
int curVersion = getPackageManager().getPackageInfo("com.XXXX.YYYY", 0).versionCode;
int newVersion = Integer.valueOf(s);
/* Is a higher version than the current already out? */
if (newVersion > curVersion) {
/* Post a Handler for the UI to pick up and open the Dialog */
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:com.XXXX.YYYY"));
startActivity(intent);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getStackTrace().toString(), Toast.LENGTH_LONG).show();
}
但是这段代码的错误是:
java.net.UnknownHostException: Unable to resolve host "play.google.com": No address associated with hostname
Unable to resolve host "play.google.com": No address associated with hostname
最佳答案
您的网络连接似乎有问题。还要确保已经在您的 AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
您上面显示的代码令人困惑。这样怎么获取版本号呢?这个要求
URL updateURL = new URL("https://play.google.com/store/apps/details?id=com.XXXX.YYYY");
将返回您的应用在 Google Play 中的 HTML 内容。未经进一步处理,您将无法获取版本。
关于android - 从 Play 商店获取当前应用程序版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27921795/