安卓下载安装更新包,各个版本注意事项

在app中大部分应用应该都需要,自动更新,就是根据版本号,来调用后端接口判断是都有最新APP,是否需要更新。那么在我们下载更新包,并安装的时候,就需要注意下,android 不同版本之间需要注意的一些问题。

  • android 6.0
    6.0以上的系统主要要注意的就是权限的动态获取。
    比如 Manifest.permission.WRITE_EXTERNAL_STORAGE 等。
  • android 7.0
    7.0以上的系统中需要注意的是文件的访问。在我们把apk下载到某个路径下的时候需要访问这个文件来安装。这时候就需要用到FileProvider 来进行配置,才能访问。
    AndroidManifest 中注册
   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.fileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths" />
    </provider>

在file_paths 文件中写入需要访问的路径

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <paths>
    <external-path path="/test_APP" name="new"/>
  </paths>
</resources>
  1. file-path 物理路径为Context.getFilesDir() + /files/*
  2. cache-path 物理路径为Context.getCacheDir() + /files/*
  3. external-path 物理路径为Environment.getExternalStorageDirectory() + /files/*
  4. external-files-path 物理路径为Context.getExternalFilesDir(String) + /files/*
  5. external-cache-path 物理路径为Context.getExternalCacheDir() + /files/*

    在调用方法中调用

 public static void openFile(Context context, File file) {

    if (file == null || !file.exists()) {
      sendDownloadError(context);
      return;
    }

    try {
      //需要对父级目录和当前文件都执行操作
      String cmd1 = "chmod 777 " + file.getParent();
      Runtime.getRuntime().exec(cmd1);

      String cmd = "chmod 777 " + file.getAbsolutePath();
      Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
      e.printStackTrace();
    }

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    File apkFile = file;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      Uri contentUri = FileProvider.getUriForFile(context,
          context.getApplicationContext().getPackageName() + ".fileProvider", apkFile);
      intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
    } else {
      intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
    }
    context.startActivity(intent);
  }
  • 8.0 系统
    在8.0以上的系统中需要,添加安装权限,并动态获取。
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值