Android 版本检测
来源:互联网 发布:php数据类型有哪些 编辑:程序博客网 时间:2024/06/15 03:54
/** * * 版本检测,自动更新 * 1.通过Url检测更新 2.下载并安装更新 3.删除临时路径 * */public class Update {// 调用更新的Activitypublic Activity activity = null;// 当前版本号public int versionCode = 0;// 当前版本名称public String versionName = "";// 控制台信息标识private static final String TAG = "AutoUpdate";// 文件当前路径private String currentFilePath = "";// 安装包文件临时路径private String currentTempFilePath = "";// 获得文件扩展名字符串private String fileEx = "";// 获得文件名字符串private String fileNa = "";// 服务器地址private String strURL = "http://127.0.0.1:8080/ApiDemos.apk";private ProgressDialog dialog;/** * * 构造方法,获得当前版本信息 * * * @param activity */public Update(Activity activity) {this.activity = activity;// 获得当前版本getCurrentVersion();}/** * * 检测更新 */public void check() {// 检测网络if (isNetworkAvailable(this.activity) == false) {return;}// 如果网络可用,检测到新版本if (true) {// 弹出对话框,选择是否需要更新版本showUpdateDialog();}}/** * * 检测是否有可用网络 * * * @param context * * @return 网络连接状态 */public static boolean isNetworkAvailable(Context context) {try {ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);// 获取网络信息NetworkInfo info = cm.getActiveNetworkInfo();// 返回检测的网络状态return (info != null && info.isConnected());} catch (Exception e) {e.printStackTrace();return false;}}/** * * 弹出对话框,选择是否需要更新版本 */public void showUpdateDialog() {@SuppressWarnings("unused")AlertDialog alert = new AlertDialog.Builder(this.activity).setTitle("新版本").setIcon(R.drawable.ic_launcher).setMessage("是否更新?").setPositiveButton("是", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {// 通过地址下载文件downloadTheFile(strURL);// 显示更新状态,进度条showWaitDialog();}}).setNegativeButton("否", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.cancel();}}).show();}/** * * 显示更新状态,进度条 */public void showWaitDialog() {dialog = new ProgressDialog(activity);dialog.setMessage("正在更新,请稍候...");dialog.setIndeterminate(true);dialog.setCancelable(true);dialog.show();}/** * * 获得当前版本信息 */public void getCurrentVersion() {try {// 获取应用包信息PackageInfo info = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);this.versionCode = info.versionCode;this.versionName = info.versionName;} catch (NameNotFoundException e) {e.printStackTrace();}}/** * * 截取文件名称并执行下载 * * * @param strPath */private void downloadTheFile(final String strPath) {// 获得文件文件扩展名字符串fileEx = strURL.substring(strURL.lastIndexOf(".") + 1, strURL.length()).toLowerCase();// 获得文件文件名字符串fileNa = strURL.substring(strURL.lastIndexOf("/") + 1,strURL.lastIndexOf("."));try {if (strPath.equals(currentFilePath)) {doDownloadTheFile(strPath);}currentFilePath = strPath;new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {// 执行下载doDownloadTheFile(strPath);} catch (Exception e) {Log.e(TAG, e.getMessage(), e);}}}).start();} catch (Exception e) {e.printStackTrace();}}/** * * 执行新版本进行下载,并安装 * * * @param strPath * * @throws Exception */private void doDownloadTheFile(String strPath) throws Exception {Log.i(TAG, "getDataSource()");// 判断strPath是否为网络地址if (!URLUtil.isNetworkUrl(strPath)) {Log.i(TAG, "服务器地址错误!");} else {URL myURL = new URL(strPath);URLConnection conn = myURL.openConnection();conn.connect();InputStream is = conn.getInputStream();if (is == null) {throw new RuntimeException("stream is null");}// 生成一个临时文件File myTempFile = File.createTempFile(fileNa, "." + fileEx);// 安装包文件临时路径currentTempFilePath = myTempFile.getAbsolutePath();FileOutputStream fos = new FileOutputStream(myTempFile);byte buf[] = new byte[128];do {int numread = is.read(buf);if (numread <= 0) {break;}fos.write(buf, 0, numread);} while (true);Log.i(TAG, "getDataSource() Download ok...");dialog.cancel();dialog.dismiss();// 打开文件openFile(myTempFile);try {is.close();} catch (Exception ex) {Log.e(TAG, "getDataSource() error: " + ex.getMessage(), ex);}}}/** * * 打开文件进行安装 * */private void openFile(File f) {Intent intent = new Intent();intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setAction(android.content.Intent.ACTION_VIEW);// 获得下载好的文件类型String type = getMIMEType(f);// 打开各种类型文件intent.setDataAndType(Uri.fromFile(f), type);// 安装activity.startActivity(intent);}/** * * 删除临时路径里的安装包 */public void delFile() {Log.i(TAG, "The TempFile(" + currentTempFilePath + ") was deleted.");File myFile = new File(currentTempFilePath);if (myFile.exists()) {myFile.delete();}}/** * * 获得下载文件的类型 * * * @param f * * 文件名称 * * @return 文件类型 */private String getMIMEType(File f) {String type = "";// 获得文件名称String fName = f.getName();// 获得文件扩展名String end = fName.substring(fName.lastIndexOf(".") + 1, fName.length()).toLowerCase();if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {type = "audio";} else if (end.equals("3gp") || end.equals("mp4")) {type = "video";} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")|| end.equals("jpeg") || end.equals("bmp")) {type = "image";} else if (end.equals("apk")) {type = "application/vnd.android.package-archive";} else {type = "*";}if (end.equals("apk")) {} else {type += "/*";}return type;}}
- Android版本检测/自动更新
- Android版本检测\自动更新
- Android版本检测\自动更新
- Android版本检测\自动更新
- Android版本检测\自动更新
- Android版本检测/自动更新
- Android版本检测\自动更新
- Android 版本检测
- Android 版本检测
- Android 检测版本更新
- Android检测版本更新
- Android版本检测升级
- Android版本检测升级
- Android版本检测升级
- Android版本检测升级
- Android检测版本更新
- Android检测版本更新
- Android 版本检测更新
- sqlserver 数据类型格式转换
- android登录和设置IP/端口功能
- mysql 慢查询
- 获取时间
- 黑客进阶必读书《Linux命令行与Shell脚本编程大全》(第2版)
- Android 版本检测
- CLR线程池的I/O线程
- 十年
- jQuery学习笔记6 模糊查询过滤器
- 计算几何 作编程参考
- Access 模糊查询的小细节
- jquery的replace
- POJ 3415 Common Substrings(后缀数组+单调栈)
- 使用 Post 方法传递数据