您的位置:首页 > 移动开发 > Android开发

Android 版本更新

2016-03-04 11:59 399 查看
import android.app.Activity;
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.Handler;
import android.os.Looper;
import android.view.KeyEvent;

import com.taihe.mplus.GloableParams;
import com.taihe.mplus.api.Api;
import com.taihe.mplus.api.net.CallbackListener;
import com.taihe.mplus.bean.ResultObjectData;
import com.taihe.mplus.bean.Version;
import com.taihe.mplus.widget.CustomDialog;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.ClientProtocolException;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;

/**
* @outher 孙磊磊
* create at 2016/2/26 17:32
* description 版本更新控制类
*/
public class VersionManager {
public VersionManager(Context context) {
this.mContext = context;
}

private Context mContext;

//安装apk
protected void installApk(File file) {
Intent intent = new Intent();
//执行动作
intent.setAction(Intent.ACTION_VIEW);
//执行的数据类型
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
mContext.startActivity(intent);
((Activity) mContext).finish();
}
/**
* 返回当前程序版本名
*/
public static String getAppVersionName(Context context) {
String versionName = "";
int versioncode;
try {
// ---get the package info---
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
versionName = pi.versionName;
versioncode = pi.versionCode;
if (versionName == null || versionName.length() <= 0) {
return "";
}
} catch (Exception e) {
L.e("VersionInfo", "Exception"+e.getMessage());
}
return versionName;
}
/**
* @author 孙磊磊
* create at 2016/2/27 10:48
* description 检查版本信息
*/
public void CheckVersion() {
Map<String, String> params = new HashMap<String, String>();
if (GloableParams.user != null) {
params.put("memberCode", GloableParams.user.getMemberCode());
}
params.put("vCode", getAppVersionName(mContext));
params.put("cinemaCode", GloableParams.cinema_code);
params.put("channelCode", "1");
params.put("appType", "0");
RequestUtils.executeRequest(Api.VERSION, params, new CallbackListener() {
@Override
public void onSuccess(String data) {
ResultObjectData<Version> resultObjectData = new ResultObjectData<Version>(data);
if (resultObjectData.isSucess()) {
Version version = resultObjectData.getResultData(Version.class);
if( !getAppVersionName(mContext).equals(version.getvCode()))
{
dialog(version);
}
}
}

@Override
public void onFailure(String message) {
}
});
}

/**
* @author 孙磊磊
* create at 2016/2/27 10:48
* description 弹窗,是否更新
*/
protected void dialog( Version version ) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(version.getvContent());
builder.setTitle(version.getvTitle());
builder.setCancelable(false);
builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return true;
}
});
builder.setPositiveButton("更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
downFile(version.getDownLoadPath());
}
});
if(version.getForceUpdate().equals("0"))
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}

/**
* @author 孙磊磊
* create at 2016/2/27 10:49
* description 下载apk
*/
void downFile(final String url) {
final ProgressDialog pBar = new ProgressDialog(mContext);    //进度条,在下载的时候实时更新进度,提高用户友好度
pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pBar.setCancelable(false);
pBar.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return true;
}
});
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar.setProgress(0);
pBar.setProgressNumberFormat("%1d kb/%2d kb");
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
int length = (int) entity.getContentLength();   //获取文件大小
pBar.setMax(length/1000);                            //设置进度条的总长度
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
final File file = new File(FileUtils.getSDCardPath() + "/taihe/image/" + System.currentTimeMillis() + ".apk");
if (is != null) {
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int process = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
process += ch;
pBar.setProgress(process/1000);       //这里就是关键的实时更新进度了!
}

}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
pBar.cancel();
if (file.length() > 0)
installApk(file);
}
});
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: