当我的应用程序打开时,我想检查google play上的应用程序版本。如果app的版本高于安装的app,我想通知用户更新App。我发现"android-query“jar来自here,在这个我不能动态检查这个版本,我想我应该设置主要,次要或修订。有没有人请帮帮我,我该怎么办?
提前感谢
发布于 2015-11-26 03:36:07
基本上,你应该检查市场上应用程序的最新版本和设备上的应用程序版本,并决定是否有可用的更新。要做到这一点,请尝试这样做:
你应该使用这个来获取当前版本(设备上的应用程序版本):
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;
}使用这个来获取谷歌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库。
Extra:要导入jsoup库,请执行以下步骤:
1-转到文件菜单
2-项目结构
3-左侧单击应用程序
4-选择依赖项选项卡
5-点击+
6-单击库依赖项
7-搜索"jsoup“
8-选择org.jsoup:jsoup并单击ok
发布于 2016-05-15 09:32:08
如果你不想使用像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();
}
}如果您将"&hl=en“保留在URL中,则字符编码UTF-8应该是可以的。
发布于 2018-05-30 15:52:53
在下面的当前播放页面的@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;
}*这不是一个通用的答案,因为它可能会随着播放页面的更改而更改*
https://stackoverflow.com/questions/18050837
复制相似问题