Android 自动检测更新apk版本
来源:互联网 发布:推荐手机支架 知乎 编辑:程序博客网 时间:2024/05/22 12:38
Android 自动检测更新apk版本
1)、功能类实现public class UpdateManager { private ProgressBar mProgressBar;//进度条 private TextView tv1; private TextView tv2; private Dialog mDownloadDialog;//对话框 private String mSavePath;//apk保存地址 private int mProgress;//进度值 private boolean mIsCancel = false;//是否取消下载标示符 private static final int DOWNLOADING = 1;//apk下载中 private static final int DOWNLOAD_FINISH = 2;//apk下载完毕 private float length; private static final String PATH = "http://www.jcpeixun.com/app_client_api/app_version.aspx";//更新地址 private String mVersion_code;//软件升级标示号 private String mVersion_name = "开心ing.apk";//apk名 private String mVersion_desc;//更新详情 private String mVersion_path;//apk下载地址 private Context mContext;//上下文 public UpdateManager(Context context) { mContext = context; } private Handler mGetVersionHandler = new Handler(){ public void handleMessage(Message msg) { JSONObject jsonObject = (JSONObject) msg.obj; System.out.println(jsonObject.toString()); Log.e("----------",jsonObject.toString()); try { mVersion_code = jsonObject.getString("version"); //mVersion_name = jsonObject.getString("version_name"); //mVersion_desc = jsonObject.getString("version_desc"); mVersion_path = jsonObject.getString("downurl"); Log.e("version ",jsonObject.getString("version")); Log.e("downurl ",jsonObject.getString("downurl")); if (isUpdate()){ //Toast.makeText(mContext, "需要更新", Toast.LENGTH_SHORT).show(); // 显示提示更新对话框 showNoticeDialog(); } else{ Toast.makeText(mContext, "已是最新版本", Toast.LENGTH_SHORT).show(); } } catch (Exception e){ e.printStackTrace(); } }; }; private Handler mUpdateProgressHandler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what){ case DOWNLOADING: // 设置进度条 mProgressBar.setProgress(mProgress); tv1.setText( mProgress + "/100"); float len = length/1024/1024;//将length转换为M单位 float b = (float)(Math.round(len*100))/100;//保留两位小数点 tv2.setText( b+"M"); break; case DOWNLOAD_FINISH: // 隐藏当前下载对话框 mDownloadDialog.dismiss(); // 安装 APK 文件 installAPK(); break; default: break; } }; }; /* * 检测软件是否需要更新 */ public void checkUpdate() { RequestQueue requestQueue = Volley.newRequestQueue(mContext); JsonObjectRequest request = new JsonObjectRequest(PATH, null, new Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { Message msg = Message.obtain(); msg.obj = jsonObject; mGetVersionHandler.sendMessage(msg); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { System.out.println(arg0.toString()); } }); requestQueue.add(request); } /* * 与本地版本比较判断是否需要更新 */ protected boolean isUpdate() { //int serverVersion = Integer.parseInt(mVersion_code); double serverVersion = Double.parseDouble(mVersion_code); double localVersion = 1; try { //获得当前apk版本号versionCode localVersion = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } if (serverVersion > localVersion) return true; else return false; } /* * 有更新时显示提示对话框 */ protected void showNoticeDialog() { AlertDialog.Builder builder = new Builder(mContext); builder.setTitle("提示"); String message = "软件有更新,要下载安装吗?\n" + mVersion_desc; builder.setMessage(message); builder.setPositiveButton("更新", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 隐藏当前对话框 dialog.dismiss(); // 显示下载对话框 showDownloadDialog(); } }); builder.setNegativeButton("下次再说", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 隐藏当前对话框 dialog.dismiss(); } }); builder.create().show(); } /* * 显示正在下载对话框 */ protected void showDownloadDialog() { AlertDialog.Builder builder = new Builder(mContext); builder.setTitle("下载中"); View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_progress, null); mProgressBar = (ProgressBar) view.findViewById(R.id.id_progress); tv1 = (TextView) view.findViewById(R.id.tv1); tv2 = (TextView) view.findViewById(R.id.tv2); builder.setView(view); // 设置按钮(左) builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 隐藏当前对话框 dialog.dismiss(); // 设置下载状态为取消 mIsCancel = true; } }); // 设置按钮(右) builder.setNeutralButton("隐藏", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); mDownloadDialog = builder.create(); mDownloadDialog.show(); // 下载文件 downloadAPK(); } /* * 开启新线程下载文件 */ private void downloadAPK() { new Thread(new Runnable() { @Override public void run() { try{//检查sd是否挂载 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ String sdPath = Environment.getExternalStorageDirectory() + "/"; mSavePath = sdPath + "MyDownload"; File dir = new File(mSavePath); if (!dir.exists()) dir.mkdir(); // 下载文件 HttpURLConnection conn = (HttpURLConnection) new URL(mVersion_path).openConnection(); conn.connect(); InputStream is = conn.getInputStream(); length = conn.getContentLength(); File apkFile = new File(mSavePath, mVersion_name); FileOutputStream fos = new FileOutputStream(apkFile); int count = 0; byte[] buffer = new byte[1024]; while (!mIsCancel){ int numread = is.read(buffer); count += numread; // 计算进度条的当前位置 mProgress = (int) ((count/length) * 100); // 更新进度条 mUpdateProgressHandler.sendEmptyMessage(DOWNLOADING); // 下载完成 if (numread < 0){ mUpdateProgressHandler.sendEmptyMessage(DOWNLOAD_FINISH); break; } fos.write(buffer, 0, numread); } fos.close(); is.close(); } }catch(Exception e){ e.printStackTrace(); } } }).start(); } /* * 下载到本地后执行安装 */ protected void installAPK() { File apkFile = new File(mSavePath, mVersion_name); if (!apkFile.exists()) return; Intent intent = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file://" + apkFile.toString()); intent.setDataAndType(uri, "application/vnd.android.package-archive"); mContext.startActivity(intent); }}2)、进度条对话框布局dialog_progress.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ProgressBar android:id="@+id/id_progress" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="14dp" android:layout_marginLeft="7dp" android:layout_marginRight="7dp" android:layout_marginTop="5dp" android:progress="14" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="14dp" android:orientation="horizontal"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_weight="1" android:gravity="left|center" android:text="左" android:textSize="16dp" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginRight="16dp" android:layout_weight="1" android:gravity="right|center" android:text="右" android:textSize="16dp" /> </LinearLayout></LinearLayout>3)、调用实现new UpdateManager(this).checkUpdate();4)、原项目视频地址:http://www.jikexueyuan.com/course/1607.html5)、效果图展示:
1 0
- Android 自动检测更新apk版本
- 自动检测更新apk版本问
- android apk版本自动检测升级,安装
- android apk版本自动检测升级,安装
- android apk版本更新
- android apk版本更新
- Android apk版本更新
- android apk版本更新
- iOS自动检测版本更新
- android版本自动检测更新 版本检测 自动更新 自动安装
- Android应用更新-自动检测版本及自动升级
- Android之Apk版本更新
- Android APK版本更新下载
- Android自动检测更新
- Android自动检测更新功能
- 自动检测版本更新的流程
- iOS 自动检测版本更新APP
- android 自动检测版本升级
- 图像分割的三类方法
- 12. spring4.1.6+spring mvc4.1.6+spring data jpa1.7.0+maven整合
- 从头认识java-9.10 Map
- MongDB启动警告WARNING:softrlimitstoolow
- stm32笔记01——重读《STM32F103x8中容量增强型产品数据手册(中文版)》
- Android 自动检测更新apk版本
- python flask框架
- Java虚拟机-----运行时栈结构
- pyqt学习
- linux shell 编程 3(if then else fi 字符串空的判断)
- 设计模式系列之一:单例模式(Singleton Pattern)
- 对C++中标准程序库异常处理的认识
- IOS学习之选择器&集合视图&表视图
- 窗口可视区和其他一些参数