实例介绍
【实例截图】
【核心代码】
package com.hiyo.game.pdk.tool; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; 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.NameNotFoundException; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.util.Log; import android.webkit.URLUtil; import com.hiyo.game.pdk.activity.R; /** * Android AutoUpdate. * * lazybone/2010.08.20 * * 1.Set apkUrl. * * 2.check(). * * 3.add delFile() method in resume()\onPause(). */ public class MyAutoUpdate { public 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:81/ApiDemos.apk"; private ProgressDialog dialog; public MyAutoUpdate(Activity activity) { this.activity = activity; getCurrentVersion(); } public void check() { if (isNetworkAvailable(this.activity) == false) { return; } if (true) {// Check version. showUpdateDialog(); } } public static boolean isNetworkAvailable(Context ctx) { try { ConnectivityManager cm = (ConnectivityManager) ctx .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("Title") .setIcon(R.drawable.icon) .setMessage("Update or not?") .setPositiveButton("Update", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { downloadTheFile(strURL); showWaitDialog(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).show(); } public void showWaitDialog() { dialog = new ProgressDialog(activity); dialog.setMessage("Waitting for update..."); 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(); } } 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; Runnable r = new Runnable() { public void run() { try { doDownloadTheFile(strPath); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } } }; new Thread(r).start(); } catch (Exception e) { e.printStackTrace(); } } private void doDownloadTheFile(String strPath) throws Exception { Log.i(TAG, "getDataSource()"); if (!URLUtil.isNetworkUrl(strPath)) { Log.i(TAG, "getDataSource() It's a wrong URL!"); } 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); // vnd.... 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(); } } 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
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)