還有就是可以實施獲取當前Play商店版本一招。
public void getUpdate() {
final String appPackageName = getPackageName();
Document doc = null;
try {
doc = Jsoup.connect("https://play.google.com/store/apps/details?id=" + appPackageName).get();
} catch (IOException e) {
e.printStackTrace();
}
// Document doc = Jsoup.parse(resString);
if(doc!=null) {
Element meta = doc.select("div[itemprop=softwareVersion]").first();
String content = meta.ownText();
System.out.println(content);
Double playStoreVersion = Double.valueOf(content);
Double currentVersion = getVersionName();
if (playStoreVersion > currentVersion) {
try {
new AlertDialog.Builder(SplashScreen.this)
.setTitle("Alert")
.setMessage("Update Is Available.Do You Want To Update?")
.setIcon(R.mipmap.ic_launcher)
.setCancelable(false)
.setPositiveButton("Update",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
try {
startActivityForResult(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)), UPDATE_FROM_PLAY_STORE_REQUEST);
} catch (Exception e) {
try {
startActivityForResult(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)), UPDATE_FROM_PLAY_STORE_REQUEST);
} catch (Exception e1) {
}
}
dialog.dismiss();
}
}).setNegativeButton("Not yet", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
Handler();
}
}).show();
//startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
try {
startActivityForResult(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)), UPDATE_FROM_PLAY_STORE_REQUEST);
} catch (Exception e) {
}
}
} else {
Handler();
}
}else {
Handler();
}
}
獲取我們自己的應用程序的版本。
public Double getVersionName() {
Double VersionName = 0.0;
try {
String vName = SplashScreen.this.getPackageManager().getPackageInfo(SplashScreen.this.getPackageName(), 0).versionName;
VersionName = Double.valueOf(vName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return VersionName;
}
檢查[this](https://github.com/rampo/UpdateChecker)是否仍然有效。如果是這樣,那麼你不需要進行任何API調用。 –
@IceMAN:看起來不錯。但我仍然更喜歡更輕量級的東西。這就是爲什麼我想知道,如果我可以對Playstore進行API調用並自己獲取這些數據。 – Rohitesh
@IceMAN:所以,我查看了代碼,他們也在Playstore上爲應用程序抓取網頁。這是我最初的想法,但據我看,它不是一個非常優雅的解決方案。所以,想知道是否有API。 – Rohitesh