package me.chunsheng.hongbao.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.Toast; import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject; import me.chunsheng.hongbao.R; import java.io.ByteArrayOutputStream;
import java.io.IOException; /**
* Util for app update task.
*/
public class UpdateTask extends AsyncTask<String, String, String> {
private Context context;
private boolean isUpdateOnRelease;
//public static final String updateUrl = "https://api.github.com/repos/geeeeeeeeek/WeChatLuckyMoney/releases/latest";
public static final String updateUrl = "https://api.github.com/repos/geeeee/WeChatLuckyMoney/releases/latest"; public UpdateTask(Context context, boolean needUpdate) {
this.context = context;
this.isUpdateOnRelease = needUpdate;
if (this.isUpdateOnRelease) Toast.makeText(context, "正在检查新版本……", Toast.LENGTH_SHORT).show();
} @Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == ) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else {
// Close the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (Exception e) {
return null;
}
return responseString;
} @Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject release = new JSONObject(result); // Get current version
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), );
String version = pInfo.versionName; String latestVersion = release.getString("tag_name");
boolean isPreRelease = release.getBoolean("prerelease");
if (!isPreRelease && version.compareToIgnoreCase(latestVersion) >= ) {
// Your version is ahead of or same as the latest.
if (this.isUpdateOnRelease)
Toast.makeText(context, R.string.update_already_latest, Toast.LENGTH_SHORT).show();
} else {
if (!isUpdateOnRelease) {
Toast.makeText(context, context.getString(R.string.update_new_seg1) + latestVersion + context.getString(R.string.update_new_seg3), Toast.LENGTH_LONG).show();
return;
}
// Need update.
String downloadUrl = release.getJSONArray("assets").getJSONObject().getString("browser_download_url"); // Give up on the fucking DownloadManager. The downloaded apk got renamed and unable to install. Fuck.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(downloadUrl));
browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(browserIntent);
Toast.makeText(context, context.getString(R.string.update_new_seg1) + latestVersion + context.getString(R.string.update_new_seg2), Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
if (this.isUpdateOnRelease)
Toast.makeText(context, R.string.update_error, Toast.LENGTH_LONG).show();
}
} public void update() {
super.execute(updateUrl);
}
}

Github获取仓库最新Release版本号API的更多相关文章

  1. Github 获取仓库的releases API

    API 文档 example: 这将获取所有的版本 https://api.github.com/repos/januwA/flutter_anime_app/releases 最新版本: https ...

  2. github 获取repo 发布的版本号

    获取最新版本 https://api.github.com/repos/nickchou/paopao/releases/latest 获取版本列表 https://api.github.com/re ...

  3. git笔记之eclipse使用github远程仓库进行版本号管理

    原文地址:http://dtbuluo.com/90.html 这里记录一下eclipse开发工具中git的使用说明. 环境:centOS.eclipse-jee-kepler-SR2-linux-g ...

  4. iOS开发——源代码管理——git(分布式版本控制和集中式版本控制对比,git和SVN对比,git常用指令,搭建GitHub远程仓库,搭建oschina远程仓库 )

    一.git简介 什么是git? git是一款开源的分布式版本控制工具 在世界上所有的分布式版本控制工具中,git是最快.最简单.最流行的   git的起源 作者是Linux之父:Linus Bened ...

  5. maven snapshot和release版本号之间的差

    在使用maven过程.我们经常会在不稳定的状态有很多公共图书馆在发展阶段.需要改变在任何时间和公布,你可能有一天一次发布.经验bug时间,甚至一天公布N次要.我们知道,.maven依赖管理是基于管理的 ...

  6. 使用命令创建github代码仓库,push本地仓库到github远程代码仓库

    1.利用命令创建github远程代码仓库 在将本地代码push到github远程代码仓库之前,总是需要新建github代码仓库,在将本地仓库关联到github远程仓库.其中最为繁琐的操作是建立gith ...

  7. Android 获取当前应用的版本号和当前系统的版本号

    1.获取当前程序版本名 我们可以在AndroidManifest.xml中设置程序的版本号等,如android:versionName="1.0",那如果想在代码中获取这个版本号呢 ...

  8. Tesseract 3.04 + VS2013 配置心得(包括静态库版本号和Release版本号)

    研究Tesseract也有几个星期了 走了一些弯路 网上有非常多VS2010的配置心得 但没有VS2013的, 找到一篇之后, 又发现会有一些小问题, 这里记录下来, 也为新人提供一些帮助. Tess ...

  9. GitHub 远程仓库 de 第一次配置

    GitHub远程仓库, Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上.首先找一台电脑充当服务器的角色, 每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上 ...

随机推荐

  1. [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下 ...

  2. Python中的Copy和Deepcopy

    一,Python的对象: Python存在大量的对象,我们一般提到的对象都是C中的结构体在堆中申请的一块内存(以CPython为例),每一个对象都有ID,可以通过ID(Object)获得.对象的范围包 ...

  3. ETL概述

    转自:http://blog.csdn.net/leosoft/article/details/4279536 ETL,Extraction-Transformation-Loading的缩写,中文名 ...

  4. float的理解

    1.浮动包裹性——浮动具有让元素按displya:inline-block显示(如果没有设置宽度和高度,则它可以显示的尽量窄高 度尽量小).2.浮动破坏性——浮动元素漂浮在标准流之上(但没有脱离文档流 ...

  5. jquery find()和filter()的区别

    在写js查找当前标签下某一子元素的子元素集合时,用到了此方法 $(".flyout-trigger").mouseover(function(){ var maxnum=$(thi ...

  6. Pseudoprime numbers(POJ 3641 快速幂)

    #include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...

  7. SQL Server 2005中的分区表(四):删除(合并)一个分区

    在前面我们介绍过如何创建和使用一个分区表,并举了一个例子,将不 同年份的数据放在不同的物理分区表里.具体的分区方式为: 第1个小表:2010-1-1以前的数据(不包含2010-1-1). 第2个小表: ...

  8. DataTable类

    DataTable是一个使用非常多的类,记得我在刚刚开始学习.Net的时候就已经了解并用过这个类,但如今再来看看,才发现这个类非常之复杂,复杂表现在哪些地方呢?主要是这个类与其他很多类都有关联,也就是 ...

  9. python模块学习之random

    模块源码: Source code: Lib/random.py 文档:http://docs.python.org/2/library/random.html 常用方法: random.random ...

  10. Summary Ranges 解答

    Question Given a sorted integer array without duplicates, return the summary of its ranges. For exam ...