位置: 编程技术 - 正文
推荐整理分享android apk版本更新(android 更新apk),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:android版本更新下载安装,android 更新apk,安卓apk修改版本号,android版本更新下载安装,android 更新apk,android版本更新下载安装,android app版本更新,android app版本更新,内容如对您有帮助,希望把文章链接给更多的朋友!
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 0x: pdu.setProgress(progress); break; case 0x: // 安装apk File apkfile = new File(saveFileName); if (!apkfile.exists()) { return; } Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse(" 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 = " 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[]; while (interceptFlag) {// 点击取消就停止下载. int numread = is.read(buf); count = numread; progress = (int) (((float) count / length) * ); // 更新进度 mHandler.sendEmptyMessage(0x); if (numread <= 0) { // 下载完成通知安装 mHandler.sendEmptyMessage(0x); 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 TCP 协议 Http协议•基于TCP无状态的应用层协议•请求组成:请求行、消息报头、消息报文•响应组成:状态行、消息报头、响应正文常见请求、响应消息报头Cont
Android RelativeLayout 大属性 AndroidRelativeLayout大属性android:layout_above="@id/xxx"--将控件置于给定ID控件之上android:layout_below="@id/xxx"--将控件置于给定ID控件之下android:layout_toLeftOf="@id/xxx"-
Host admin-PC is not allowed to connect to this MySQL server 问题:Host'admin-PC'isnotallowedtoconnecttothisMySQLserver(其中,admin-PC为我的机器名)原因:安装MySQL时没有勾选Enablerootaccessfromremotemachines解决办法:执行命令[s
标签: android 更新apk
本文链接地址:https://www.jiuchutong.com/biancheng/378114.html 转载请保留说明!下一篇:Http TCP 协议
友情链接: 武汉网站建设