android 应用更新安装不成功,android应用内更新app(解决7.0以上安装问题)

7.0以上版本要注意:添加以下代码。还要再Manifast中增加Provider,在res目录下创建xml目录,在xml目录下创建file_path.xml文件

5f949d29a90b

文件下载后跳转安装

5f949d29a90b

Manifast配置

5f949d29a90b

file_path.xml

public class UpdateTaskextends AsyncTask {

private Contextcontext;

private ProgressDialogpd; //进度条对话框

public UpdateTask(Context context) {

this.context = context;

}

@Override

protected void onPreExecute() {

pd =new ProgressDialog(context);

pd.setCanceledOnTouchOutside(false);

pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

pd.setMessage("正在下载更新");

pd.setProgressNumberFormat("%1d kb/%2d kb");//进度和总数

pd.setCancelable(false);

pd.show();

super.onPreExecute();

}

@Override

protected FiledoInBackground(String... params) {

String path = params[0];

String verNew = params[1];

HttpURLConnection conn =null;

File file =null;

//如果相等的话表示当前的sdcard挂载在手机上并且是可用的

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

URL url =null;

try {

url =new URL(path);

}catch (MalformedURLException e) {

e.printStackTrace();

}

try {

if (url.getProtocol().toLowerCase().equals("https")) {

HttpsURLConnection https = (HttpsURLConnection) url.openConnection();

https.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

conn = https;

}else {

conn = (HttpURLConnection) url.openConnection();

}

conn.setConnectTimeout(10000);

//获取到文件的大小

int count = conn.getContentLength();

InputStream is = conn.getInputStream();

file =new File(Environment.getExternalStorageDirectory(), "nodepp." + verNew +".apk");//下载到本地的app名

FileOutputStream fos =new FileOutputStream(file);

BufferedInputStream bis =new BufferedInputStream(is);

byte[] buffer =new byte[1024];

int len;

int total =0;

while ((len = bis.read(buffer)) != -1) {

fos.write(buffer, 0, len);

total += len;

//获取当前下载量

publishProgress(total, count);

}

fos.close();

bis.close();

is.close();

return file;

}catch (Exception e) {

e.printStackTrace();

}finally {

}

}else {

return null;

}

return file;

}

@Override

protected void onProgressUpdate(Integer... values) {

pd.setProgress(values[0] /1024);

pd.setMax(values[1] /1024);

super.onProgressUpdate(values);

}

@Override

protected void onPostExecute(File file) {

if (file !=null) {

//安装apk

Intent intent =new Intent();

//执行动作

intent.setAction(Intent.ACTION_VIEW);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if(Build.VERSION.SDK_INT>=24) {//判读版本是否在7.0以上

//7.0以上需要通过FileProvider获取uri

Uri apkUri = FileProvider.getUriForFile(context, "com.nodepp.smartnode.fileprovider", file);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件

intent.setDataAndType(apkUri, "application/vnd.android.package-archive");

}else {

//执行的数据类型,此处Android应为android,否则造成安装不了

intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");

}

context.startActivity(intent);

pd.dismiss();

((Activity)context).finish();

}else {

pd.dismiss();

// 下载app失败

AlertDialog dialog =new AlertDialog.Builder(context).setTitle("提示")

.setMessage("应用下载失败")

.setCancelable(false)

.setPositiveButton("确定",

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int which) {

dialog.dismiss();

}

}).create();

dialog.show();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值