android 最简单的版本更新


//解析通过Gson得到bean对象,对象中有url地址 和versonCode版本号
bean =gson.fromJson(msg,VersonBean.class);
String mcode=CommonParams.getCode();
if(bean.getVersonCode().equals(mcode)){
   return;
}else{
   AlertDialog.Builder builder=new AlertDialog.Builder(this);
   builder.setTitle("提示更新");
   builder.setIcon(R.drawable.warn);// 设置提示的title的图标,默认是没有的)
   builder.setMessage("检测到最新版本,是否更新");
   builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
           if (ContextCompat.checkSelfPermission(MainActivity.this,
                   Manifest.permission.WRITE_EXTERNAL_STORAGE)
                   != PackageManager.PERMISSION_GRANTED) {
               ActivityCompat.requestPermissions(MainActivity.this,
                       new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                       MY_PERMISSIONS_REQUEST_CALL_PHONE);
           } else {
               if (bean == null || bean.getUrl().length() == 0) {
                   return;
               }
               downloadApk();//下载
           }
       }
   });
   builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
           dialog.dismiss();
       }
   });
   builder.show();

}

/**
 * 下载apk
 */
private void downloadApk() {
  
ProgressDialog  pd6 = new ProgressDialog(this);
pd6.setProgressStyle(ProgressDialog. STYLE_HORIZONTAL) ; // 设置水平进度条 pd6.setCancelable( false) ; // 设置是否可以通过点击Back键取消 pd6.setCanceledOnTouchOutside( false) ; // 设置在点击Dialog外是否取消Dialog进度条 pd6.setTitle( "提示") ; pd6.setMax( 100) ; pd6.setProgress( 0) ; pd6.setMessage( "正在更新请稍后...") ; pd6.show() ; Thread downLoadThread = new Thread( mdownApkRunnable) ; downLoadThread.start() ;} //安装apk protected void installApk() { File apkfile = new File( saveFileName) ; if (!apkfile.exists()) { return; } Intent intent = new Intent() ; //执行动作 intent.setAction(Intent. ACTION_VIEW) ; //执行的数据类型 intent.setDataAndType(Uri. fromFile(apkfile) , "application/vnd.android.package-archive") ; startActivity(intent) ; finishThisPage() ;} private Runnable mdownApkRunnable = new Runnable() { @Override public void run() { try { URL url = new URL( bean.getUrl()) ; HttpURLConnection conn = (HttpURLConnection) url.openConnection() ; conn.connect() ; int length = conn.getContentLength() ; is = conn.getInputStream() ; File file = new File( savePath) ; if (!file.exists()) { file.mkdir() ; } String apkFile = saveFileName ; File ApkFile = new File(apkFile) ; fos = new FileOutputStream(ApkFile) ; int count = 0 ; byte buf[] = new byte[ 1024] ; do { int numread = is.read(buf) ; count += numread ; progress = ( int) ((( float) count / length) * 100) ; //Logger.i("logger_progress", "----------do---------------" + progress); //更新进度 mHandler.sendEmptyMessage( DOWN_UPDATE) ; mHandler.sendEmptyMessage( DOWN_YUYU) ; if (numread <= 0) { //下载完成通知安装 mHandler.sendEmptyMessage( DOWN_OVER) ; mHandler.sendEmptyMessage( DOWN_YUYUO) ; return; } fos.write(buf , 0 , numread) ; } while (! interceptFlag&&! isStop) ; //点击取消就停止下载. fos.close() ; is.close() ; isStop= true; } catch (MalformedURLException e) { e.printStackTrace() ; } catch (IOException e) { e.printStackTrace() ; } }}
 
 

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (bean == null || bean.getUrl().length()==0) {
                return;
            }
            downloadApk();
        } else {
            // Permission Denied
           ToastUtils.showLong(MainActivity.this, "未获得权限!!");
        }
        return;
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

猜你喜欢

转载自blog.csdn.net/zqj861791241/article/details/79988994