rav*_*idl 36 version-control android google-apps-marketplace google-play google-play-developer-api
如何更新Play商店应用程序,即如果用户使用旧版本应用程序,我如何从Google Play商店获取应用程序版本信息以提示用户强制/建议更新应用程序.我已经通过了andorid-market-api,这不是官方的方式,也需要谷歌的oauth登录验证.我也经历了android查询 ,它提供了应用内版本检查,但它在我的情况下不起作用.我找到了以下两种选择:
还有其他方法可以轻松完成吗?
小智 41
我建议不要使用库只是创建一个新类
1.
public class VersionChecker extends AsyncTask<String, String, String>{
String newVersion;
@Override
protected String doInBackground(String... params) {
try {
newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + "package name" + "&hl=en")
.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.hAyfc:nth-child(4) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)")
.first()
.ownText();
} catch (IOException e) {
e.printStackTrace();
}
return newVersion;
}
Run Code Online (Sandbox Code Playgroud)
在您的活动中:
VersionChecker versionChecker = new VersionChecker();
String latestVersion = versionChecker.execute().get();
Run Code Online (Sandbox Code Playgroud)就这些
这是jQuery版本,以获取版本号,如果其他人需要它.
$.get("https://play.google.com/store/apps/details?id=" + packageName + "&hl=en", function(data){
console.log($('<div/>').html(data).contents().find('div[itemprop="softwareVersion"]').text().trim());
});
Run Code Online (Sandbox Code Playgroud)
使用此代码完美正常工作.
public void forceUpdate(){
PackageManager packageManager = this.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo =packageManager.getPackageInfo(getPackageName(),0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
String currentVersion = packageInfo.versionName;
new ForceUpdateAsync(currentVersion,TodayWork.this).execute();
}
public class ForceUpdateAsync extends AsyncTask<String, String, JSONObject> {
private String latestVersion;
private String currentVersion;
private Context context;
public ForceUpdateAsync(String currentVersion, Context context){
this.currentVersion = currentVersion;
this.context = context;
}
@Override
protected JSONObject doInBackground(String... params) {
try {
latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + context.getPackageName()+ "&hl=en")
.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.hAyfc:nth-child(3) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)")
.first()
.ownText();
Log.e("latestversion","---"+latestVersion);
} catch (IOException e) {
e.printStackTrace();
}
return new JSONObject();
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
if(latestVersion!=null){
if(!currentVersion.equalsIgnoreCase(latestVersion)){
// Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show();
if(!(context instanceof SplashActivity)) {
if(!((Activity)context).isFinishing()){
showForceUpdateDialog();
}
}
}
}
super.onPostExecute(jsonObject);
}
public void showForceUpdateDialog(){
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
}
}
Run Code Online (Sandbox Code Playgroud)
我怀疑请求应用程序版本的主要原因是为了提示用户进行更新。我不赞成抓取响应,因为这可能会破坏未来版本的功能。
如果应用最低版本为5.0,可以根据文档实现应用内更新https://developer.android.com/guide/app-bundle/in-app-updates
如果请求应用程序版本的原因不同,您仍然可以使用 appUpdateManager 来检索版本并执行您想要的任何操作(例如将其存储在首选项中)。
例如,我们可以将文档的片段修改为这样的:
// Creates instance of the manager.
val appUpdateManager = AppUpdateManagerFactory.create(context)
// Returns an intent object that you use to check for an update.
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
val version = appUpdateInfo.availableVersionCode()
//do something with version. If there is not a newer version it returns an arbitary int
}
Run Code Online (Sandbox Code Playgroud)
JS 示例(可以移植到任何其他语言):
import { JSDOM } from 'jsdom';
import axios from 'axios';
const res = await axios('https://play.google.com/store/apps/details?id=com.yourapp');
const dom = new JSDOM(res.data);
const scripts = Array.from(dom.window.document.querySelectorAll('script'));
const script = scripts.find(s => s.textContent && s.textContent.includes('/store/apps/developer'));
const versionStringRegex = /"[0-9]+\.[0-9]+\.[0-9.]+"/g;
const matches = script.textContent.match(versionStringRegex);
const match = matches[0];
const version = match.replace(/"/g, '');
console.log(version); // '1.2.345'
Run Code Online (Sandbox Code Playgroud)
不幸的是,Play 商店最近更改了其 DOM 布局,除非您打开模式,否则版本号不可见。
然而,作为元数据埋藏在脚本标签之一中,最终呈现到该模式中的数据确实存在。它没有以任何方式键入,而是只是埋在数组中,因此该解决方案依赖于同一附近的一些不太可能更改的数据。
小智 0
我会推荐使用前。推送通知以通知您的应用程序有新的更新,或者使用您自己的服务器使您的应用程序能够从那里读取版本。
是的,每次更新应用程序时都会进行额外的工作,但在这种情况下,您不依赖于某些可能停止服务的“非官方”或第三方事物。
以防万一您错过了某些内容 - 之前对您的主题的讨论是否在 Google Play 商店中查询应用程序的版本?