在我的应用程序中,我想检查应用商店中是否有任何更新版本的应用程序。如果有,要通过提示信息通知用户,如果用户选择升级,我想更新新版本。我希望通过我的应用程序完成所有这些操作。这是否可能?
在我的应用程序中,我想要检查是否存在应用商店中的最新版本。如果有,我希望通过提示消息通知用户,并且如果他/她选择升级,我希望能够更新到最新版本。请问这种方式是否可行?在我的应用程序中,我想检查应用商店中是否有任何更新版本的应用程序。如果有,要通过提示信息通知用户,如果用户选择升级,我想更新新版本。我希望通过我的应用程序完成所有这些操作。这是否可能?
在我的应用程序中,我想要检查是否存在应用商店中的最新版本。如果有,我希望通过提示消息通知用户,并且如果他/她选择升级,我希望能够更新到最新版本。请问这种方式是否可行?我也遇到了同样的问题,但通过使用JSOUP库解决了。这是该库的下载链接:http://jsoup.org/download
String 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[itemprop=softwareVersion]").first()
.ownText();
Log.e("new Version", newVersion);
<div class="content" itemprop="softwareVersion"> 2.2.0 </div>
一旦您找到页面的这个部分,您可以提取版本号并将其与您的应用程序中可用的版本进行比较:
try
{
String version = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
if( ! version.equals(versionFromHTML))
{
Toast.makeText(this, "New version available on play store", Toast.LENGTH_SHORT);
}
}
catch (NameNotFoundException e)
{
//No version do something
}
我发现了另一种方法,我是这样做的。
HttpPost httppostUserName = new HttpPost("https://androidquery.appspot.com/api/market?app=com.supercell.clashofclans"); //your package name
HttpClient httpclient = new DefaultHttpClient();
HttpResponse responseUser = httpclient.execute(httppostUserName);
String responseStringUser = EntityUtils.toString(responseUser.getEntity(), HTTP.UTF_8);
Log.d(Constants.TAG, "Response: " + responseStringUser);
try {
JSONObject Json = new JSONObject(responseStringUser);
newVersion = Json.getString("version");
} catch (Exception e) {
e.printStackTrace();
}
如果您将URL粘贴到浏览器中查看结果,您将获得更清晰的视图。
dependencies {
implementation 'com.google.android.play:core:1.5.0'
...
}
编辑
Ahmad Arlan的答案目前是最好的答案。但如果你到这里并尝试剪切和粘贴他的代码,你会遇到我刚刚遇到的同样的问题,所以我也可以在这里发布它来帮助像我一样的其他人。
Make sure you have INTERNET
permission on your AndroidManifest.xml
file.
<uses-permission android:name="android.permission.INTERNET"/>
Add JSOUP
dependency to your module build.gradle
.
dependencies {
compile 'org.jsoup:jsoup:1.10.2'
}
Surround the snippet with try and catch
and don't run it on the main thread.
public class MainActivity extends AppCompatActivity {
String newVersion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new FetchAppVersionFromGooglePlayStore().execute();
}
class FetchAppVersionFromGooglePlayStore extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
try {
return
Jsoup.connect("https://play.google.com/store/apps/details?id=" + "com.directed.android.smartstart" + "&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[itemprop=softwareVersion]")
.first()
.ownText();
} catch (Exception e) {
return "";
}
}
protected void onPostExecute(String string) {
newVersion = string;
Log.d("new Version", newVersion);
}
}
}
implementation 'org.jsoup:jsoup:1.8.3'
try {
String currentVersion="";
currentVersion = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
Log.e("Current Version","::"+currentVersion);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
new GetVersionCode().execute();
private class GetVersionCode extends AsyncTask<Void, String, String> {
@Override
protected String doInBackground(Void... voids) {
String newVersion = null;
try {
Document document = 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();
if (document != null) {
Elements element = document.getElementsContainingOwnText("Current Version");
for (Element ele : element) {
if (ele.siblingElements() != null) {
Elements sibElemets = ele.siblingElements();
for (Element sibElemet : sibElemets) {
newVersion = sibElemet.text();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return newVersion;
}
@Override
protected void onPostExecute(String onlineVersion) {
super.onPostExecute(onlineVersion);
if (onlineVersion != null && !onlineVersion.isEmpty()) {
if (onlineVersion.equals(currentVersion)) {
} else {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Update");
alertDialog.setIcon(R.mipmap.ic_launcher);
alertDialog.setMessage("New Update is available");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Update", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + context.getPackageName())));
}
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
Log.d("update", "Current version " + currentVersion + "playstore version " + onlineVersion);
}
}
Google Play 已经实现了这个功能。当您上传应用的新版本后,它会直接向用户设备发送通知或在用户在 Google Play 应用中开启了自动更新选项后会自动下载升级。
更新2019 Android Dev Summit
Google推出了应用内更新库。它适用于Lollipop+,并且可以让您使用漂亮的对话框(灵活)或强制全屏消息(立即)向用户请求更新。
应用内更新仅适用于运行Android 5.0(API级别21)或更高版本的设备。
private void checkForUpdate() {
PackageInfo packageInfo = null;
try { packageInfo=getPackageManager().getPackageInfo(DashboardActivity.this.getPackageName(), 0);
int curVersionCode = packageInfo.versionCode
if (curVersionCode > 1) { // instead of one use value get from server for the new update.
showUpdateDialog();
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}