android版本更新问题
2013-12-31 13:39
337 查看
做版本更新大多数情况会是在一个对话框里,在这里写的就是前些日子做的版本更新。
首先需要判断一下手机的内存卡是否可用:
下面就是开始下载了:
到此,主要部分已经贴出来了,需要主要的是OOM问题(这里应该不会出现什么问题,我已经测过多种机型了)。
首先需要判断一下手机的内存卡是否可用:
public static boolean isAvailable() { return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); }
下面就是开始下载了:
public void startDownload(final String strPath) { Runnable r = new Runnable() { public void run() { try { doDownloadTheFile(strPath); openFile();//下载完成打开apk文件 } catch (Exception e) { // Log.e(TAG, e.getMessage(), e); } } }; new Thread(r).start(); }
private FileOutputStream setOutPutStream(){ FileOutputStream outStream = null; try { if(isAvailable()){ outStream = new FileOutputStream(saveFileName,false); }else{//如果没有sd卡,那也不能不让人家下载更新啊,就下载到手机内存里吧。 outStream=mContext.openFileOutput("FightLandlord.apk",Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE); } } catch (Exception e) { // TODO: handle exception } return outStream; }
public void doDownloadTheFile(String strPath) throws Exception { if (!URLUtil.isNetworkUrl(strPath)) { Log.i("LogUtils", "下载地址错误!"); } else { URL myURL = new URL(strPath); URLConnection conn = myURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); fileSize = conn.getContentLength(); FileOutputStream outputStream = setOutPutStream(); byte[] buffer = new byte[1024]; downLoadSize = 0; int len; try { while ((len = is.read(buffer)) != -1) { outputStream.write(buffer, 0, len); downLoadSize += len; Message message1 = new Message(); message1.what = 0; handler.sendMessage(message1);//此handler用于更新进度 } } catch (OutOfMemoryError e) { Toast.makeText(mContext, "内存不足!", Toast.LENGTH_SHORT).show(); outputStream.close(); is.close(); e.printStackTrace(); } try { outputStream.close(); is.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
private void openFile() { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); if(isAvailable()){ intent.setDataAndType(Uri.parse("file://" + saveFileName.toString()), "application/vnd.android.package-archive"); }else{ intent.setDataAndType(Uri.fromFile(new File(mContext.getFilesDir().getAbsolutePath()+"/FightLandlord.apk")), "application/vnd.android.package-archive"); } mContext.startActivity(intent); }
Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { if (msg.what == 0) { progressBar.setProgress(downLoadSize * 100/fileSize); progressText.setText(downLoadSize*100/fileSize+"%"); if(downLoadSize/fileSize >= 1){ dismiss();//下载完成对话框消失 } } }; };
到此,主要部分已经贴出来了,需要主要的是OOM问题(这里应该不会出现什么问题,我已经测过多种机型了)。
相关文章推荐
- Android 8.0版本更新无法自动安装问题的解决方法
- android关于版本更新问题(2)
- Android 版本更新签名冲突的问题以及注意事项,已解决
- Android 版本更新下载自动安装APK,并解决Android6.0,7.0安装失败的问题
- Android版本相机适配问题集合(不断整理更新中)
- Android版本更新时对SQLite数据库升级或者降级遇到的问题
- 关于android中版本自动更新,从服务器下载下来的文件不能安装的问题
- Android Studio3.0.1版本更新后遇到的问题
- Android版本更新时对SQLite数据库升级或者降级遇到的问题
- 更新Android SDK到3.0版本时,遇到Failed to rename directory E:\android\tools to E:\android\temp\ToolPackage.old01问题
- 对Android中的apk文件进行数字签名及版本更新时遇到apk签名问题
- Android版本更新时对SQLite数据库升级或者降级遇到的问题
- 【Android】两步搞定AndroidSDKManager在线更新SDK版本失败问题
- Android studio appcompat-v7 更新23.0.x版本后出现问题
- 关于Android 2.2与2.3 adb.exe版本更新问题(在安装android——SDK时候找不到相应的 /tools/adb.exe! )
- Android中关于APP的版本更新问题
- Android 版本更新(非热更新) 适配7.0更新 以及三星 note系列读取内存相关目录无权限问题
- 【Android】(已更新解决方案)更新SDK版本(25)后,自定义FloatingActionButton的Behavior(跟随列表滑动显示隐藏)只隐藏不出现的问题
- 从 xUtils 中发现的Android 6.0 版本更新问题
- [转]更新Android SDK到3.0版本时,遇到Failed to rename directory E:\android\tools to E:\android\temp\ToolPackage.old01问题