android6.0的apk更新

android6.0以上的版本,写入权限不仅需要在   manifest文件里书写,还需要进行动态申请。   

if (Build.VERSION.SDK_INT >= 23) {
    int REQUEST_CODE_CONTACT = 101;
    String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};//验证是否许可权限
    for (String str : permissions) {
        if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
            //申请权限
            this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
            break;
        }
    }
}

            

android6.0以上的apk更新安装,采用fileprovider的方法。

1.现在manifest中添加provider

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.qijin.bluetoothapplication.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

2.书写xml资源文件。

<paths>
    <external-path path="Android/data/包名/" name="files_root" />
    <external-path path="." name="external_storage_root" />
</paths>

3.更改安装。

private void installApk() {
      File apkfile = new File(saveFileName);
      if (!apkfile.exists()) {
         return;
      }
      Intent intent = new Intent(Intent.ACTION_VIEW);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
         intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
         Uri contentUri = FileProvider.getUriForFile(mContext, "com.qijin.bluetoothapplication.fileprovider", apkfile);
         intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
      } else {
         intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      }
//    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//    intent.setDataAndType(Uri.parse("file://" + apkfile.toString()),
//          "application/vnd.android.package-archive");
      mContext.startActivity(intent);
      callback.OnBackResult("finish");

   }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值