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

来源:互联网 发布:单片机和fpga的区别 编辑:程序博客网 时间:2024/05/21 15:45

 

 /**
     * 下载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);
      
    }



 

 

 

 

原创粉丝点击