付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026学年鲁迅的《故乡》教学设计
- T/CI 1410-2026透明陶瓷激光损伤阈值的测定规范
- T/CCIA 0024-2024氢化改性环氧美缝剂
- 2025年福建省建瓯市高二生物上册期末考试测试卷【名校卷】附答案
- T/JSJTQX 42-2023公路连续梁桥整联同步顶升施工技术规程
- 2026年吉林省扶余市高二历史上册期末考试自测卷【必考】附答案
- 2026年浙江省海宁市高二历史上册期末考试检测卷附完整答案【名校卷】
- 2026年黑龙江省安达市高考历史测试卷及参考答案【突破训练】
- 2026年四川省彭州市高二历史上册期末考试模拟卷附答案【综合题】
- 2026年吉林省集安市高二生物上册期末考试检测卷含答案【典型题】
- 江苏水利工程设计概(估)算编制规定2017年修订版
- 污水处理PLC控制系统方案设计指南
- 设施设备维护人员面试题及答案
- 1105 非无菌产品微生物限度检查:2020年版 VS 2025年版对比表
- 医务人员职业暴露试题(附答案)
- 公路施工项目安全风险评估报告范本
- 安静的力量主题班会课件
- 《2025型钢采购合同》
- 全麻术后导尿管刺激征管理
- 以例启思:高中思想政治教学中案例教学法的深度应用与策略探索
- 高等职业学校空中乘务专业 实训教学条件建设标准
评论
0/150
提交评论