1.4 应用自动升级安装(下载和安装)

 

  /**
     * 下载APK的方法
     */
      public   void   showUpdateDialog(){
      AlertDialog.Builder  builder=   new   Builder(   this );
      builder.setTitle(   "更新版本:" + versionBean   .getVersion());
      builder.setMessage(   versionBean .getDescription());
      builder.setPositiveButton(   "立刻升级" ,   new   OnClickListener() {
                 public   void   onClick(DialogInterface dialog,   int   which) {
                        
                  
                     if (!!Environment.   MEDIA_MOUNTED .equals(Environment. getExternalStorageState())){     //判断SD卡是否存在
                    
                      //升级下载
                     FinalHttp   http=   new   FinalHttp();
                     File file=   new   File(Environment.getExternalStorageDirectory(),   "mobilesafe.apk" );
                  
                     http.download( versionBean .getPath(),file.getAbsolutePath(),   new   AjaxCallBack<File>(){
                        
                            @Override
                            public   void   onLoading(   long   count,   long   current) {
                        
                            tv_splash_progress .setTag(   "下载进度:" +(current*100)/count);
                            super .onLoading(count, current);
                       
                        
                          }
                             
                              @Override
                              public   void   onSuccess(File t) {
                              
                              //下载完成时,安装SD卡中的 apk
                            installApk(t);
                              
                                 super .onSuccess(t);
                              
                              
                            } 
                     
                  
                  });
                    
                  
                    
                     }   else {
                           Toast. makeText(getApplicationContext(),   "SD卡不可用..." , 0).show();
                           loadMainUI();    //进入主界面
                     }
                        
                  }
            });
      
      
      builder.setNegativeButton(   "下次再说" , new   OnClickListener() {
                  
                     @Override
                     public   void   onClick(DialogInterface dialog,   int   which) {
                        
                        loadMainUI();    //进入主界面
                  }
            } );
      builder.show();
      
    }



-----------------------------------------------------------------------------------------

  /**
     *
     * 安装 apk的方法
     */
      public   void   installApk(File file){
      
         /*<action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <data android:scheme="content" />
         <data android:scheme="file" />
         <data android:mimeType="application/vnd.android.package-archive" />
      */
      Intent intent= new   Intent();
      intent.setAction(   "android.intent.action.VIEW" );
      intent.addCategory( "android.intent.category.DEFAULT"   );
      intent.setDataAndType(Uri. fromFile(file),   "application/vnd.android.package-archive"   );
      startActivity(intent);
      
    }



 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值