安卓弹出对话框和安装

这是我第一次上传博客,谢谢支持!



 private void showMyDialog(final updateBean data) {
               //获得对话框
               final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                //设置对话框的提醒标题
                builder.setTitle("提示");
                //设置对话框的主要内容
                builder.setMessage(data.desc);

               //设置确认按钮,复写setPositiveButton方法
                builder.setPositiveButton("下载", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //下载App方法(单独抽取出来)
                        downloadAPP(data);
                    }
                });

                //设置取消按钮,复写setNegativeButton方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        startHomeView();
                    }
                });

                //设置关闭对话框的监听.复写onCancel方法
                builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        startHomeView();
                    }
                });
                //显示对话框(记得必须要写)
                builder.show();
            }

  private void downloadAPP(updateBean data) {
      //新建HttpUtils对象
      HttpUtils httpUtils = new HttpUtils();
      //设置文件保存的路径,这里保存到SD卡目录,名字为xxx.apk
      final File SD_Card_Path = new File(Environment.getExternalStorageDirectory(), "xxx.apk");
      //设置httpUtils下载
      /*
      *参数一:下载的地址
      *参数二:下载的路径,getAbsolutePath()为绝对路径
      *参数三:
      **/
      httpUtils.download(data.downloadurl, SD_Card_Path.getAbsolutePath(), false, new RequestCallBack<File>() {

      @Override//成功后调用的方法,调出安装界面,一般是固定的.
      public void onSuccess(ResponseInfo<File> responseInfo) {
          //新建意图对象
          Intent intent = new Intent();
          intent.setAction("android.intent.action.VIEW");
          intent.addCategory("android.intent.category.DEFAULT");
          intent.setDataAndType(Uri.fromFile(SD_Card_Path), "application/vnd.android.package-archive");
          //开始意图,结束后最终做的事情,调用onActivityResult方法(在下面)
          startActivityForResult(intent, 0);
  }

      @Override//下载失败调用的方法
     public void onFailure(HttpException e, String s) {
       Toast.makeText(MainActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
       startHomeView();
                    }
                });
            }
        };
    }

    @Override//最终做的事情
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        startHomeView();
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值