如何检查新的更新是否可用于Play商店。有没有简单的方法来获得这个。
限制。 1.没有自定义网络服务。
可以使用
任何帮助将不胜感激。
答案 0 :(得分:1)
使用此辅助方法在Play商店中查找更新版本
private String getPlayStoreVersion(){
final String PLAY_STORE_URL = "https://play.google.com/store/apps/details?id=%s&hl=%s";
final String PLAY_STORE_TAG_RELEASE = "itemprop=\"softwareVersion\">";
String version = "0.0.0.0";
Boolean isAvailable = false;
String source = "";
OkHttpClient client = new OkHttpClient();
String formedString = String.format(PLAY_STORE_URL,"com.demo.application.yourapp",Locale.getDefault().getLanguage());
ResponseBody body = null;
try {
URL playStoreURL = new URL(formedString);
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
body = response.body();
BufferedReader reader = new BufferedReader(new InputStreamReader(body.byteStream(), "UTF-8"));
StringBuilder str = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
if (line.contains(PLAY_STORE_TAG_RELEASE)) {
str.append(line);
isAvailable = true;
}
}
response.body().close();
source = str.toString();
if (isAvailable) {
String[] splitPlayStore = source.split(PLAY_STORE_TAG_RELEASE);
if (splitPlayStore.length > 1) {
splitPlayStore = splitPlayStore[1].split("(<)");
version = splitPlayStore[0].trim();
}
}
return version;
} catch(Exception e){
e.printStackTrace();
}
return version;
}
注意:在后台线程中使用
答案 1 :(得分:1)
所以我认为我有一个简单的解决方案。
需要依赖。
compile 'org.jsoup:jsoup:1.7.3'
示例代码
class GetVersionCode extends AsyncTask<Void, String, String> {
private static final String currentVersion = "x.x.xx";
static final String packageName = "com.xxx.xxx";
@Override
protected String doInBackground(Void... voids) {
String newVersion = null;
try {
newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + packageName + "&hl=it")
.timeout(30000)
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get()
.select("div[itemprop=softwareVersion]")
.first()
.ownText();
return newVersion;
} catch (Exception e) {
// HTTP error fetching URL, 404
return newVersion;
}
}
@Override
protected void onPostExecute(String onlineVersion) {
super.onPostExecute(onlineVersion);
if (null == onlineVersion) {
// Some error occurred
return;
}
if (onlineVersion.compareTo("") == 0) {
// Some error occurred
return;
}
Toast.makeText(getApplicationContext(), onlineVersion, Toast.LENGTH_SHORT).show();
}
}