![【移动应用开发技术】android 应用更新升级模块 升级会卡 引发CF_第1页](http://file4.renrendoc.com/view/e05500797825237325e93650f55a14c6/e05500797825237325e93650f55a14c61.gif)
![【移动应用开发技术】android 应用更新升级模块 升级会卡 引发CF_第2页](http://file4.renrendoc.com/view/e05500797825237325e93650f55a14c6/e05500797825237325e93650f55a14c62.gif)
![【移动应用开发技术】android 应用更新升级模块 升级会卡 引发CF_第3页](http://file4.renrendoc.com/view/e05500797825237325e93650f55a14c6/e05500797825237325e93650f55a14c63.gif)
![【移动应用开发技术】android 应用更新升级模块 升级会卡 引发CF_第4页](http://file4.renrendoc.com/view/e05500797825237325e93650f55a14c6/e05500797825237325e93650f55a14c64.gif)
![【移动应用开发技术】android 应用更新升级模块 升级会卡 引发CF_第5页](http://file4.renrendoc.com/view/e05500797825237325e93650f55a14c6/e05500797825237325e93650f55a14c65.gif)
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】android应用更新升级模块升级会卡引发CF
今天在做应用升级的模块的时候,给大家分享一下升级的相关代码模块,在升级的过程前期的下载,和现在弹出的相关窗体这些的没有什么难度的,就是有个重要的地方,在每次要刷新下载进度更新的时候,要给个有条件更新,如果每次够让他跟新的,会导致应用边卡,引发CF等问题,下面是相关的代码分享,若其他问题可以私聊我!!!
本应用用到了两个开源库:
需要的人找我私聊publicclassMainActivityextendsActivity{
privateintmCurVersion;
privateObjUpDataupdata;
privatestaticNotificationManagermNm;
privatestaticRemoteViewsmrRemoteViews;
privatestaticNotificationnotification;
privatestaticinttmp;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//
点击下载
findViewById(R.id.button1).setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
updatautil();
}
});
//若点击下载后,在点别的会倒是CF
findViewById(R.id.button2).setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
Toast.makeText(MainActivity.this,"卡了!!我就呵呵了",Toast.LENGTH_SHORT).show();
}
});
}
publicintgetCurrentVersion(){
PackageInfoinfo=null;
try{
PackageManagerpm=getPackageManager();
info=pm.getPackageInfo(getPackageName(),0);
}catch(NameNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
Log.e("getCurrentVersion","info.versionCode="+info.versionCode);
returninfo.versionCode;
}
privatevoidupdatautil(){
//小模块:实现版本升级
//打开应用
//>有新版本,弹出dialog(更新了xx功能,修复了xxbug,提升自动定位的准确性...);
//如果点击取消就不更新,
//点击确定才开始下载最新安装包
//>下载完成,下拉点击,跳转到安装界面
//有新版本,弹出dialog
//获取当前版本
mCurVersion=getCurrentVersion();
//获取新版本,下载文件,解析得到文件中所有数据
HttpUtil.post("03:8080/tins//equRgController/doDownloadNewVersion.do",//下载路径
newRequestParams(),newAsyncHttpResponseHandler(){
@Override
publicvoidonSuccess(intstatusCode,Header[]headers,Stringcontent){
Log.e("HttpUtil.post","statusCode="+statusCode+"headers="+headers+"content="+content);
UpDatadata=newGson().fromJson(content,UpData.class);
updata=data.getObj();
//得到新版本号
intnewVersion=updata.getVersion();
//将新版本号跟就版本进行比较
if(newVersion>mCurVersion){
//弹出dialog提示是否需要更新并显示更新简介
showMyDialog();
}
}
@Override
publicvoidonFailure(intstatusCode,Header[]headers,Throwableerror,Stringcontent){
}
});
}
privatevoidshowMyDialog(){
finalDialogmDialog=newDialog(this);
mDialog.setTitle("发现新版本");
Viewview=getLayoutInflater().inflate(R.layout.dialog_item,null);
TextViewtextView=(TextView)view.findViewById(R.id.textView1);
textView.setText(updata.getDesc());
view.findViewById(R.id.button2).setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
mDialog.dismiss();
newMyAsyn(mNm).execute(updata.getLoadUrl());
ShowPedding();
}
});
view.findViewById(R.id.button1).setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
mDialog.dismiss();
}
});
mDialog.setContentView(view);
mDialog.show();
}
//启动异步下载
staticclassMyAsynextendsAsyncTask<String,Integer,Integer>{
privateNotificationManagermNm;
publicMyAsyn(NotificationManagermNm){
this.mNm=mNm;
}
@Override
protectedIntegerdoInBackground(String...params){
intlength=0;
FileOutputStreamfos=null;
try{
URLurl=newURL(params[0]);
URLConnectionopenConnection=url.openConnection();
InputStreamis=openConnection.getInputStream();
length=openConnection.getContentLength();
byte[]buffer=newbyte[1024];
intend=0;
intsum=0;
Log.e("doInBackground","Environment.getExternalStorageDirectory().getPath()="
+Environment.getExternalStorageDirectory().getPath());
fos=newFileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/TTins.apk");
//fos=newFileOutputStream("/mnt/sdcard/TTins.apk");
while(-1!=(end=is.read(buffer))){
fos.write(buffer,0,end);
intresent=sum*100/length;
sum+=end;
if(resent%6==0&&tmp!=resent){
tmp=resent;
publishProgress(length,sum,resent);
}
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(fos!=null){
try{
fos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnlength;
}
@Override
protectedvoidonProgressUpdate(Integer...values){
mrRemoteViews.setProgressBar(R.gressBar1,values[0],values[1],false);
mrRemoteViews.setTextViewText(R.id.textView1,"已下载"+values[2]+"%");
mNm.notify(1,notification);
super.onProgressUpdate(values);
}
@Override
protectedvoidonPostExecute(Integerresult){
mrRemoteViews.setProgressBar(R.gressBar1,result,result,false);
mrRemoteViews.setTextViewText(R.id.textView1,"下载完成");
mNm.notify(1,notification);
super.onPostExecute(result);
}
}
privatevoidShowPedding(){
//privatestaticvoidShowPedding(Activityactivity,
//NotificationManagermNm){
StringfileName=Environment.getExternalStorageDirectory().getAbsolutePath()+"/TTins.apk";
//创建URI
Uriuri=Uri.fromFile(newFile(fileName));
Intentintent=newIntent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,"application/vnd.android.package-archive");
PendingIntentpendingIntent=PendingIntent.getActivity(this,0,intent,0);
mrRemoteViews=newRemoteViews(getPackageName(),R.layout.remote_item);
notification=newNotificationCompat.Builder(this).setTicker("开始下载").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent).setContent(mrRemoteViews).build();
mNm.notify(1,(Notification)notification);
}
}弹出dialog的XML文件<RelativeLayoutxmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#ffffff"
android:layout_alignParentTop="true">
<ImageView
android:id="@+id/p_w_picpathView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/p_w_picpathView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="LargeText"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="23dp"
android:layout_toLeftOf="@+id/p_w_picpathView1"
android:text="取消"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/p_w_picpathView1"
android:text="确定"/>
</RelativeLayout></RelativeLayout>下面分享一下要用的两个XML文件<RelativeLayoutxmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toLeftOf="@+id/button1"
android:layout_toRightOf="@+id/p_w_picpathView1"/>
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业信息转换过程中的权限管理
- 升学的申请书
- 学生会退出申请书
- 2024-2025学年山东省威海市文登区高三上学期第一次模拟物理试题(解析版)
- 部编版小学语文2021年真卷四年级下册期末试卷(含答案)
- 绿化管理合同(2篇)
- 新版北师版一年级下册数学课件六 有趣的平面图形(一)第5课时 拼图大挑战
- 电动汽车充电设施的智能调度系统研究
- 外研版高中英语选择性必修第四册UNIT2 Period5课件
- 一建《法规及相关知识》试题库资料练习含【参考答案-】卷28
- 2025年工贸企业春节复工复产方案
- 安防监控工程施工方案(3篇)
- 2025年蓝莓种苗行业深度研究分析报告
- 【道法】历久弥新的思想理念课件 2024-2025学年统编版道德与法治七年级下册
- 《糖尿病诊疗规范》课件
- 2025年度消防工程安全防护措施设计固定总价合同范本3篇
- 2025年事业单位财务工作计划(三篇)
- Unit 2 Know your body(说课稿)-2024-2025学年外研版(三起)(2024)英语三年级下册
- 名师工作室建设课件
- 《电子技术应用》课程标准(含课程思政)
- 纸尿裤使用管理制度内容
评论
0/150
提交评论