我想在我的应用程序打开时检查Google Play上的应用程序版本。如果App的版本高于已安装的应用程序,我想通知用户更新应用程序。我发现来自here的“android-query”jar,在此我无法动态检查版本,我想设置Major,Minor或Revision。有人请帮帮我,我该怎么办?
提前致谢
答案 0 :(得分:12)
基本上,您应该检查市场中应用的最新版本以及设备上的应用版本,并确定是否有可用的更新。为此,请尝试:
您应该使用它来获取当前版本(设备上的应用程序版本):
private String getCurrentVersion(){
PackageManager pm = this.getPackageManager();
PackageInfo pInfo = null;
try {
pInfo = pm.getPackageInfo(this.getPackageName(),0);
} catch (PackageManager.NameNotFoundException e1) {
e1.printStackTrace();
}
String currentVersion = pInfo.versionName;
return currentVersion;
}
并使用此功能获取Google Play中的最新版本(取自https://stackoverflow.com/a/32942785/444991):
private class GetLatestVersion extends AsyncTask<String, String, String> {
String latestVersion;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
//It retrieves the latest version by scraping the content of current version from play store at runtime
String urlOfAppFromPlayStore = "https://play.google.com/store/apps/details?id= your app package address";
Document doc = Jsoup.connect(urlOfAppFromPlayStore).get();
latestVersion = doc.getElementsByAttributeValue("itemprop","softwareVersion").first().text();
}catch (Exception e){
e.printStackTrace();
}
return latestVersion;
}
}
然后,当您的应用启动时,请按照以下方式对彼此进行检查:
String latestVersion = "";
String currentVersion = getCurrentVersion();
Log.d(LOG_TAG, "Current version = " + currentVersion);
try {
latestVersion = new GetLatestVersion().execute().get();
Log.d(LOG_TAG, "Latest version = " + latestVersion);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//If the versions are not the same
if(!currentVersion.equals(latestVersion)){
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("An Update is Available");
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Click button action
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=your app package address")));
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Cancel button action
}
});
builder.setCancelable(false);
builder.show();
}
显示用户更新对话框。但请确保您已导入jsoup库。
额外:要导入jsoup库,请按照下列步骤操作:
1-转到文件菜单
2-项目结构
3-左侧点击应用程序
4-选择依赖项选项卡
5-单击+
6-点击图书馆依赖关系
7-搜索&#34; jsoup&#34;
8-选择org.jsoup:jsoup并点击确定
答案 1 :(得分:5)
如果您不想使用像Jsoup这样的库,可以使用类似的方法从Google Play获取当前版本号:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
public class StackAppVersion {
public static void main(String[] args) {
try {
System.out.println(currentVersion());
} catch (IOException ex) {
System.out.println("Failed to read Google Play page!");
}
}
private static String currentVersion() throws IOException {
StringBuilder sb = new StringBuilder();
try (Reader reader = new InputStreamReader(
new URL("https://play.google.com/store/apps/details?id=com.stackexchange.marvin&hl=en")
.openConnection()
.getInputStream()
, "UTF-8"
)) {
while (true) {
int ch = reader.read();
if (ch < 0) {
break;
}
sb.append((char) ch);
}
} catch (MalformedURLException ex) {
// Can swallow this exception if your static URL tests OK.
}
String parts[] = sb.toString().split("softwareVersion");
return parts[1].substring(
parts[1].indexOf('>') + 1, parts[1].indexOf('<')
).trim();
}
}
如果你保留&#34;&amp; hl = en&#34;在URL中,UTF-8的字符编码应该没问题。
答案 2 :(得分:2)
接受的答案不再有效。最近我不得不改变代码。
尝试以下方法:
Jsoup.connect("https://play.google.com/store/apps/details?id=APPPACKAGE&hl=en")
.timeout(10000)
.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:contains(Current Version)").last().parent()
.select("span").last()
.ownText();
答案 3 :(得分:2)
在@Mohammad回答中添加一点修改,对于当前的游戏页面,下面对我有效。
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect("https://play.google.com/store/apps/details?id=YOUR_PACKAGE_NAME").get();
Element element = doc.getElementsByClass("BgcNfc").get(3);
latestVersion = element.parent().children().get(1).children().text();
} catch (Exception e) {
latestVersion = currentVersion;
}
return latestVersion;
}
***这不是一般答案,因为它可能会随播放页面的变化而改变*
答案 4 :(得分:0)
经过大量的在线搜索后,我设法找到了解决方案!所以对于那里寻找正确答案的其他人来说
protected String doInBackground(Void ... void){
StringBuilder sb = new StringBuilder();
try (Reader reader = new InputStreamReader(
new URL("https://play.google.com/store/apps/details?id="+BuildConfig.APPLICATION_ID+"&hl=en")
.openConnection()
.getInputStream()
, "UTF-8"
)) {
while (true) {
int ch = reader.read();
if (ch < 0) {
break;
}
sb.append((char) ch);
}
} catch (MalformedURLException ex) {
Log.d("ERROR", ex.getMessage());
return null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.d("ERROR", e.getMessage());
return null;
} catch (IOException e) {
e.printStackTrace();
Log.d("ERROR", e.getMessage());
return null;
}
String parts[] = sb.toString().split("Current Version");
String res = parts[1].substring(
parts[1].indexOf("htlgb") + 7, parts[1].indexOf("htlgb") + 11
).trim().toString();
return res;
}