自动更新工具类
来源:互联网 发布:windows可上网镜像 编辑:程序博客网 时间:2024/06/16 08:22
public class DownLoadUtil {public static void checkNewVersion(final Activity context, final boolean isShowInfo) {RequestParams params = new RequestParams();params.put("PlatID", 1);params.put("Number", CommonUtil.getVersion(context));HttpUtil.asyncGet(API.URL_GET_APP_VERSION_INFO, params, new JsonHttpResponseHandler() {@Overridepublic void onStart() {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.showLoadingMessage(context);}}@Overridepublic void onSuccess(int statusCode, Header[] headers, final JSONObject response) {// TODO Auto-generated method stubif (!CommonUtil.checkStatus(response)) {if (isShowInfo) {AlertUtil.showErrorMessage(context, response.optString("Message"));}return;}if (response.optJSONObject("Result") != null) {showNewVersion(context, response);} else {if (isShowInfo) {AlertUtil.showInfoMessage(context, "已经是最新版本");}}}@Overridepublic void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.showErrorMessage(context, "服务器异常");}}@Overridepublic void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.showErrorMessage(context, "服务器异常");}}@Overridepublic void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.showErrorMessage(context, "服务器异常");}}@Overridepublic void onFinish() {// TODO Auto-generated method stubif (isShowInfo) {AlertUtil.dismiss(context);}}});}public static void showNewVersion(final Activity activity, final JSONObject response) {JSONObject result = response.optJSONObject("Result");if(result == null){return;}int enforceUpdate = result.optInt("EnforceUpdate");final String url = result.optString("DownloadUrl");if(enforceUpdate == 0){AlertUtil.AlertDialog(activity, "发现新版本可用,是否进行更新?", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubload(activity, url, dialog,response);}});}else{AlertUtil.AlertDialogPositive(activity, "提示", "发现新版本,需进行更新才可使用", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubload(activity, url, dialog,response);}});}}public static void load(final Activity activity, final String url, DialogInterface dialog, final JSONObject response) {dialog.dismiss();if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {activity.runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubAlertUtil.showErrorMessage(activity, "下载失败,SD卡不可用,下载失败");}});return;}final ProgressDialog pd = new ProgressDialog(activity);pd.setTitle("更新");pd.setMessage("正在下载");pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pd.setCancelable(false);pd.setCanceledOnTouchOutside(false);pd.show();new Thread() {public void run() {File file = new File(Environment.getExternalStorageDirectory(),"guopu_" + System.currentTimeMillis() + ".apk");file = CommonUtil.download(pd, url, file.getAbsolutePath());if (file != null) {// 下载成功installApk(activity, file);} else {// 下载失败activity.runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubAlertUtil.showErrorMessage(activity, "下载失败",new android.view.View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint enforceUpdate = response.optJSONObject("Result").optInt("EnforceUpdate");if(enforceUpdate == 1){showNewVersion(activity, response);}}});}});}pd.dismiss();}}.start();}/** * 安装一个应用程序 * * @param file */public static void installApk(Activity activity, File file) {Intent intent = new Intent();/* * <action android:name="android.intent.action.VIEW" /> <category * android:name="android.intent.category.DEFAULT" /> <data * android:scheme="content" /> <data android:scheme="file" /> <data * android:mimeType="application/vnd.android.package-archive" /> */intent.setAction("android.intent.action.VIEW");intent.addCategory("android.intent.category.DEFAULT");intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");activity.startActivity(intent);activity.finish();}}
0 0
- 自动更新工具类
- 关于ShareSyncher共享文件自动更新工具
- 关于游戏自动更新工具的思考
- unity编辑器扩展 SVN自动更新工具
- android自动更新工作类
- 自动更新
- 自动更新
- 自动更新
- 自动更新
- 自动更新
- 自动更新
- 自动更新
- 自动更新
- 自动更新
- 自动更新
- 自动更新
- Tomcat自动更新class类文件
- 数据库根据实体类自动更新
- 独木舟上的旅行(南阳oj71)(贪心)
- 《写给25岁的我们》
- Spring mvc——接收对象Json数据,<mvc:annotation-driven/><context:annotation-config/><context:component-scan/>
- C#反射是什么
- JqMobi 基础知识
- 自动更新工具类
- Tomcat--系统架构与设计模式,第 1 部分: 工作原理
- Spring常用属性的注入及属性编辑器
- Java初涉之6--浅谈反射
- 十二月英语学习总结-希冀
- 判断网络是否可用
- LeetCode 257:Binary Tree Paths
- 报错: org.apache.commons.pool2.impl.GenericObjectPoolConfig not found
- java网络协议(IPV4/IPV6)