152 lines
5.0 KiB
Plaintext
Executable File
152 lines
5.0 KiB
Plaintext
Executable File
package com.cm.utils.update;
|
|
|
|
import android.app.AlertDialog;
|
|
import android.app.ProgressDialog;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.net.Uri;
|
|
import android.os.Environment;
|
|
import android.os.SystemClock;
|
|
|
|
import com.cm.utils.net.HttpUtils;
|
|
import com.cm.utils.net.response.DownloadResponseHandler;
|
|
import com.cm.utils.uiutils.UIUtil;
|
|
|
|
import java.io.File;
|
|
|
|
/**
|
|
* Created by Xuer on 2017/3/9.
|
|
*/
|
|
public class CheckUpdateUtils {
|
|
/**
|
|
* 根据版本号大小判断是否需要更新
|
|
*
|
|
* @param context
|
|
* @param versionCode 服务端传回来的版本号
|
|
* @return
|
|
*/
|
|
public static boolean checkcode(Context context,String versionCode){
|
|
PackageInfo packageInfo = null;
|
|
try {
|
|
packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
boolean isupdate = packageInfo.versionCode < Integer.parseInt(versionCode) ? true : false;
|
|
return isupdate;
|
|
}
|
|
/**
|
|
* 根据版本名称不同判断是否需要更新
|
|
*
|
|
* @param context
|
|
* @param versionName 服务端传回来的版本名称
|
|
* @returntring
|
|
*/
|
|
public static boolean check(Context context,String versionName){
|
|
PackageInfo packageInfo = null;
|
|
try {
|
|
packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
boolean isupdate = packageInfo.versionName.equals(versionName) ? false : true;
|
|
return isupdate;
|
|
}
|
|
|
|
/**
|
|
* 检测更新
|
|
* @param context
|
|
* @param versionCode
|
|
* @param downloadUrl
|
|
* @param strategy
|
|
*/
|
|
public static void checkUpdate(final Context context, int versionCode, final String downloadUrl, String strategy) {
|
|
// if (check(context,versionCode)) {
|
|
if(strategy.equals("1")){
|
|
downloadApk(downloadUrl,context);
|
|
}else{
|
|
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
builder.setMessage("是否更新");
|
|
builder.setCancelable(false);
|
|
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
//下载新版本
|
|
CheckUpdateUtils.downloadApk(downloadUrl, context);
|
|
|
|
}
|
|
});
|
|
builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
dialog.dismiss();
|
|
}
|
|
});
|
|
builder.show();
|
|
}
|
|
// }
|
|
|
|
}
|
|
|
|
/**
|
|
* 下载新版本dpk
|
|
|
|
* @param downloadUrl 下载地址
|
|
*/
|
|
public static void downloadApk(String downloadUrl, final Context context) {
|
|
final ProgressDialog dialog=new ProgressDialog(context);
|
|
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
|
dialog.setCancelable(false);
|
|
//文件存放地址
|
|
String dir = Environment.getExternalStorageDirectory().toString();
|
|
//文件下载dialog
|
|
dialog.show();
|
|
|
|
HttpUtils.getInstance().download(downloadUrl, dir, SystemClock.uptimeMillis() + ".apk", new DownloadResponseHandler() {
|
|
@Override
|
|
public void onFinish(File download_file) {
|
|
if (download_file!=null){
|
|
installApkNew(context, Uri.fromFile(download_file));
|
|
}else{
|
|
onFailure("");
|
|
}
|
|
|
|
dialog.dismiss();
|
|
}
|
|
|
|
@Override
|
|
public void onProgress(long currentBytes, long totalBytes) {
|
|
dialog.setMax((int) totalBytes);
|
|
dialog.setProgress((int) currentBytes);
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(String error_msg) {
|
|
dialog.dismiss();
|
|
UIUtil.toast(context, "新版本下载失败!");
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 安装apk
|
|
*
|
|
* @param context
|
|
* @param uri //文件uri
|
|
*/
|
|
protected static void installApkNew(Context context, Uri uri) {
|
|
Intent intent = new Intent();
|
|
//执行动作
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
//执行的数据类型
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
intent.setDataAndType(uri, "application/vnd.android.package-archive");
|
|
context.startActivity(intent);
|
|
}
|
|
|
|
}
|