这是我第一次上传博客,谢谢支持!
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();
}
安卓弹出对话框和安装
最新推荐文章于 2021-05-26 02:30:45 发布