android apk版本更新
android apk版本更新
/**
* 获取版本名称
*
* @return version
*/
private String getVersionName() {
try {
// 获取packagemanager的实例
PackageManager packageManager = getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(
getPackageName(), 0);
version = packInfo.versionName;// versionName是获取版本名称还有版本号等等...
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return version;
}
activity类调用
// 这里来检测版本是否需要更新
UpdateManager mUpdateManager = new UpdateManager(
SettingActivity.this);
mUpdateManager.checkUpdateInfo();
public class UpdateManager {
private Context mContext;
/* 下载包安装路径 */
private static final String savePath = "//sdcard//updatedemo//";
private static final String saveFileName = savePath + "licaike.apk";
/* 进度条与通知ui刷新的handler和msg常量 */
private int progress;
private ProgressDialogUtil pdu;
private boolean interceptFlag = true;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x123:
pdu.setProgress(progress);
break;
case 0x124: // 安装apk
File apkfile = new File(saveFileName);
if (!apkfile.exists()) {
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
"application/vnd.android.package-archive");
mContext.startActivity(i);
pdu.dismiss();
break;
default:
break;
}
};
};
public UpdateManager(Context context) {
this.mContext = context;
}
// 外部接口让主Activity调用
public void checkUpdateInfo() {
final AlertDialogUtil dialogUtil = new AlertDialogUtil(mContext, false,
null);
dialogUtil.setMessage("请更新新版本");
dialogUtil.setBtnPositiveValue("更新");
dialogUtil.setPositiveClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
showDownloadDialog();
dialogUtil.dismiss();
}
});
dialogUtil.setBtnNegativeValue("取消");
dialogUtil.setNegativeClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
dialogUtil.dismiss();
}
});
dialogUtil.show();
}
private void showDownloadDialog() {
// 下载apk
downLoadThread();
pdu = new ProgressDialogUtil(mContext, false, null);
pdu.setMessage("软件正在更新中...");
pdu.setBtnNegativeValue("取消");
pdu.setNegativeClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
interceptFlag = false;
pdu.dismiss();
}
});
pdu.show();
}
private void downLoadThread() {
new Thread(new Runnable() {
@Override
public void run() {
InputStream is = null;
FileOutputStream fos = null;
try {
// 返回的安装包url
String apkUrl = "http://d.m.hexun.com/app/licaike.apk";
URL url = new URL(apkUrl);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.connect();
int length = conn.getContentLength();
is = conn.getInputStream();
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
String apkFile = saveFileName;
File ApkFile = new File(apkFile);
fos = new FileOutputStream(ApkFile);
int count = 0;
byte buf[] = new byte[1024];
while (interceptFlag) {// 点击取消就停止下载.
int numread = is.read(buf);
count += numread;
progress = (int) (((float) count / length) * 100);
// 更新进度
mHandler.sendEmptyMessage(0x123);
if (numread <= 0) {
// 下载完成通知安装
mHandler.sendEmptyMessage(0x124);
break;
}
fos.write(buf, 0, numread);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
if (is != null)
is.close();
} catch (Exception e) {
} finally {
fos = null;
is = null;
}
}
}
}).start();
}
}
AlertDialog 工具类我的博客里写过了,你可以看看自己动手写,也可以粘贴过来用
http://blog.csdn.net/menglele1314/article/details/45718501
进度条你自己随便往对话框拖个
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。