如何让用户从应用程序内检查最新的应用程序版本?

我想在应用程序中添加“检查更新”button,这样当有人点击它时,它会显示一个敬酒信息/进度对话框来检查应用程序的版本。

如果find新版本,应用程序将自动将其下载到手机,并让用户手动安装更新的应用程序。

或者只要能检查最新版本并通知用户更新就可以做其他任何方法

如果它是市场上的应用程序,那么在应用程序启动时,启动一个意图打开市场应用程序,希望这将导致它检查更新。

否则,实现和更新检查器是相当容易的。 这是我的代码(大致):

String response = SendNetworkUpdateAppRequest(); // Your code to do the network request // should send the current version // to server if(response.equals("YES")) // Start Intent to download the app user has to manually install it by clicking on the notification startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("URL TO LATEST APK"))); 

当然,你应该重写这个在后台线程上做请求,但是你明白了。

如果你喜欢一些但更复杂的东西,但允许你的应用程序自动应用更新,请看这里 。

为了节省时间编写检查Android应用程序的新版本更新,我把它写成库和开源在https://github.com/winsontan520/Android-WVersionManager

你可以使用这个Android库: https : //github.com/danielemaddaluno/Android-Update-Checker 。 它旨在提供一个可重复使用的工具来asynchronous检查,如果在商店中存在任何更新的您的应用程序发布的更新。 它基于使用Jsoup( http://jsoup.org/ )来testing是否真正存在parsingGoogle Play商店中的应用程序页面的新更新:

 private boolean web_update(){ try { String curVersion = applicationContext.getPackageManager().getPackageInfo(package_name, 0).versionName; String newVersion = curVersion; 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(); return (value(curVersion) < value(newVersion)) ? true : false; } catch (Exception e) { e.printStackTrace(); return false; } } 

而作为“价值”function以下(如果价值0至99之间的作品):

 private long value(String string) { string = string.trim(); if( string.contains( "." )){ final int index = string.lastIndexOf( "." ); return value( string.substring( 0, index ))* 100 + value( string.substring( index + 1 )); } else { return Long.valueOf( string ); } } 

如果你只想validation版本之间的不匹配,你可以改变:

value(curVersion) < value(newVersion)value(curVersion) != value(newVersion)

导航到您的播放页面:

 https://play.google.com/store/apps/details?id=com.yourpackage 

使用标准的HTTP GET。 现在下面的jQuery为你find重要的信息:

当前版本

 $("[itemprop='softwareVersion']").text() 

什么是新的

 $(".recent-change").each(function() { all += $(this).text() + "\n"; }) 

现在您可以手动提取这些信息,只需在您的应用程序中为您执行此操作。

 public static String[] getAppVersionInfo(String playUrl) { HtmlCleaner cleaner = new HtmlCleaner(); CleanerProperties props = cleaner.getProperties(); props.setAllowHtmlInsideAttributes(true); props.setAllowMultiWordAttributes(true); props.setRecognizeUnicodeChars(true); props.setOmitComments(true); try { URL url = new URL(playUrl); URLConnection conn = url.openConnection(); TagNode node = cleaner.clean(new InputStreamReader(conn.getInputStream())); Object[] new_nodes = node.evaluateXPath("//*[@class='recent-change']"); Object[] version_nodes = node.evaluateXPath("//*[@itemprop='softwareVersion']"); String version = "", whatsNew = ""; for (Object new_node : new_nodes) { TagNode info_node = (TagNode) new_node; whatsNew += info_node.getAllChildren().get(0).toString().trim() + "\n"; } if (version_nodes.length > 0) { TagNode ver = (TagNode) version_nodes[0]; version = ver.getAllChildren().get(0).toString().trim(); } return new String[]{version, whatsNew}; } catch (IOException | XPatherException e) { e.printStackTrace(); return null; } } 

使用HtmlCleaner

没有API,你不能自动安装它,你可以redirect到它的市场页面,以便他们可以升级。 您可以在Web服务器上的文件中包含最新版本,并让应用程序检查它。 这里有一个这样的实现:

http://code.google.com/p/openintents/source/browse/#svn%2Ftrunk%2FUpdateCheckerApp

您应该首先检查市场上的应用程序版本,并将其与设备上的应用程序版本进行比较。 如果它们不同,则可能是可用的更新。 在这篇文章中,我写下了获取设备上当前版本的市场和当前版本的代码,并将它们进行比较。 我还展示了如何显示更新对话框并将用户redirect到更新页面。 请访问此链接: https : //stackoverflow.com/a/33925032/5475941

APP LEVEL build.gradle中添加compile 'org.jsoup:jsoup:1.10.2'

只要添加下面的代码,你很好去。

 private class GetVersionCode extends AsyncTask<Void, String, String> { @Override protected String doInBackground(Void... voids) { try { newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + SplashActivity.this.getPackageName() + "&hl=it") .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(); return newVersion; } catch (Exception e) { return newVersion; } } @Override protected void onPostExecute(String onlineVersion) { super.onPostExecute(onlineVersion); if (!currentVersion.equalsIgnoreCase(onlineVersion)) { //show dialog new AlertDialog.Builder(context) .setTitle("Updated app available!") .setMessage("Want to update app?") .setPositiveButton("Update", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // continue with delete final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object try { Toast.makeText(getApplicationContext(), "App is in BETA version cannot update", Toast.LENGTH_SHORT).show(); startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); } catch (ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); } } }) .setNegativeButton("Later", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // do nothing dialog.dismiss(); new MyAsyncTask().execute(); } }) .setIcon(android.R.drawable.ic_dialog_alert) .show(); } } }