博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Github获取仓库最新Release版本号API
阅读量:6836 次
发布时间:2019-06-26

本文共 4050 字,大约阅读时间需要 13 分钟。

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
{ 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[0])); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == 200) { 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(), 0); String version = pInfo.versionName; String latestVersion = release.getString("tag_name"); boolean isPreRelease = release.getBoolean("prerelease"); if (!isPreRelease && version.compareToIgnoreCase(latestVersion) >= 0) { // 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(0).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); }}

 

转载地址:http://plqkl.baihongyu.com/

你可能感兴趣的文章
nginx常用功能全揭秘
查看>>
Spark(四) -- Spark工作机制
查看>>
CSS3中的选择器
查看>>
追求极致的AI·OS——AI·OS引擎平台
查看>>
Spark PruneDependency 依赖关系 RangePartitioner
查看>>
Java与Excel的交互!-
查看>>
使用 WebIDE 三分钟上手函数计算
查看>>
一. synchronized 的局限性 与 Lock 的优点
查看>>
大龄码农经验那么丰富,为什么很多公司都不招?
查看>>
一辈子不用考试?你可能是个假程序员
查看>>
利用WSS搭建学生作业平台
查看>>
刚进入win7系统就提示检测到一个硬盘问题的解决方法
查看>>
Python之配置日志模块logging
查看>>
指定目录的所有 *.gif 文件都重命名为 *.jpg
查看>>
为11.2.0.2 Grid Infrastructure添加节点
查看>>
Linux运维课程 第一阶段 重难点摘要(六)CISCO
查看>>
inotify结合rsync监控目录的实时变化
查看>>
pfSense book之硬件配置指南
查看>>
存储过程总结 2
查看>>
C#高性能大容量SOCKET并发(十):SocketAsyncEventArgs线程模型
查看>>