首页 > 解决方案 > 在 SDK 26 中从本地存储安装 APK 时出现问题

问题描述

我有一种方法可以进行应用程序更新,该方法在 sdk 版本 26 之前可以完美运行。

在 26 中,它打开 Activity,然后立即再次关闭它,而不会引发任何(明显的)异常或警告。

我没有看到任何有关新权限或任何必要内容的文档。如果我只是将 minSdkVersion 更改为 25,它会再次正常工作。

很想知道 26 有什么变化以及如何解决它。

这是相关的代码段,但正如我所说,它并没有“损坏”——它只是在 26 中默默地失败了。

if (Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.M) {
    Log.d(TAG, "Marshmallow or lower");
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
//if version 24 or above
else {
    Log.d(TAG, "higher than Marshmallow");

    Uri uri = FileProvider.getUriForFile(mContext, mContext.getApplicationContext().getPackageName() + ".provider", file);
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
mContext.startActivity(intent);

标签: androidandroid-versionapp-update

解决方案


您是否尝试将以下权限添加到清单?

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

Android Oreo 中进行了一些更改,以使从 Playstore 外部安装应用程序更安全。您可以在以下链接获取更多信息:

https://android-developers.googleblog.com/2017/08/making-it-safer-to-get-apps-on-android-o.html


推荐阅读