当前位置: 首页 > 科技观察

安卓应用自动更新

时间:2023-03-23 10:33:03 科技观察

源码介绍自动更新,手机没有SD卡。我亲自测试过。源码截图源码片段intdown_step=down_step_custom;//提示stepinttotalSize;//文件总大小intdownloadCount=0;//下载大小intupdateCount=0;//上传文件大小InputStreaminputStream;OutputStreamoutputStream;URLurl=newURL(down_url);HttpURLConnectionhttpURLConnection=(HttpURLConnection)url.openConnection();httpURLConnection.setConnectTimeout(TIMEOUT);httpURLConnection.setReadTimeout(TIMEOUT);//获取下载文件的sizetotalSize=httpURLConnection.getContentLength();if(httpURLConnection.getResponseCode()==404){thrownewException("fail!");//这个地方应该加一个下载失败的处理,但是因为我们在外面加了一个try---catch,这个Exception已经处理过了,//所以不需要处理}inputStream=httpURLConnection.getInputStream();outputStream=newFileOutputStream(file,false);//如果文件存在,则覆盖bytebuffer[]=newbyte[1024];intreadsize=0;while((readsize=inputStream.read(buffer))!=-1){///********如果出现错误occurs在下载过程中,会弹出错误信息并取消notificationManager************///if(httpURLConnection.getResponseCode()==404){//notificationManager.cancel(R.layout.notification_item);//通过wnewException("fail!");////这个地方应该加一个下载失败的处理,但是因为我们在外面加了一个try---catch,这个Exception已经被处理过了,////所以不需要处理//}outputStream.write(buffer,0,readsize);down??loadCount+=readsize;//定时获取下载的大小/***每次增加3%**/if(updateCount==0||(downloadCount*100/totalSize-down_step)>=updateCount){updateCount+=down_step;//改变通知栏contentView.setTextViewText(R.id.notificationPercent,updateCount+"%");contentView.setProgressBar(R.id.notificationProgress,100,updateCount,false);notification.contentView=contentView;notificationManager.notify(R.layout.notification_item,notification);}}if(httpURLConnection!=null){httpURLConnection.disconnect();}inputStream.close();outputStream.close();返回下载次数;来源链接:http://down.51cto.com/data/2012784