xDr*_*onZ 54 java versioning android http
我想在应用程序中添加"检查更新"按钮,以便当有人单击它时,它将显示一个用于检查应用程序版本的Toast消息/进度对话框.
如果找到新版本,应用程序将自动将其下载到手机,并允许用户手动安装更新的应用程序.
或者任何其他方法都可以,只要它可以检查最新版本并通知用户更新.
win*_*520 44
为了节省用于检查Android应用程序的新版本更新的时间,我将其编写为库和开源https://github.com/winsontan520/Android-WVersionManager
mad*_*adx 38
您可以使用此Android库:https://github.com/danielemaddaluno/Android-Update-Checker.它旨在提供一种可重复使用的工具,以便在商店中存在应用程序的任何较新发布更新时异步检查.它基于使用Jsoup(http://jsoup.org/)来测试是否确实存在解析Google 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;
}
}
Run Code Online (Sandbox Code Playgroud)
并且作为"值"功能如下(如果值在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 );
}
}
Run Code Online (Sandbox Code Playgroud)
如果只想验证版本之间的不匹配,可以更改:
value(curVersion) < value(newVersion) 同 value(curVersion) != value(newVersion)
Jas*_*eer 26
如果它是市场上的应用程序,那么在应用程序启动时,启动Intent以打开市场应用程序,这将导致它检查更新.
否则,实现和更新检查器相当容易.这是我的代码(大致):
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")));
Run Code Online (Sandbox Code Playgroud)
当然你应该重写这个来在后台线程上做请求,但你明白了.
如果您喜欢一些但更复杂但允许您的应用自动应用更新的内容,请参阅此处.
Raj*_*ddy 11
Google在两个月前更新了Play商店.这是我现在正在使用的解决方案..
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=" + MainActivity.this.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 (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {
//show anything
}
}
Log.d("update", "Current version " + currentVersion + "playstore version " + onlineVersion);
}
}
Run Code Online (Sandbox Code Playgroud)
并且不要忘记添加JSoup库
dependencies {
compile 'org.jsoup:jsoup:1.8.3'}
Run Code Online (Sandbox Code Playgroud)
并在Oncreate()上
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String currentVersion;
try {
currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
new GetVersionCode().execute();
}
Run Code Online (Sandbox Code Playgroud)
就是这样..感谢这个链接
小智 5
导航到您的播放页面:
https://play.google.com/store/apps/details?id=com.yourpackage
Run Code Online (Sandbox Code Playgroud)
使用标准HTTP GET.现在,以下jQuery为您找到重要信息:
$("[itemprop='softwareVersion']").text()
Run Code Online (Sandbox Code Playgroud)
$(".recent-change").each(function() { all += $(this).text() + "\n"; })
Run Code Online (Sandbox Code Playgroud)
现在您可以手动提取这些信息,只需在您的应用程序中创建一个为您执行此操作的方法.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
添加 compile 'org.jsoup:jsoup:1.10.2' 到APP LEVEL build.gradle 中的依赖项
&
只需添加下面的代码,你就可以开始了。
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
92789 次 |
| 最近记录: |