如何在 Android Oreo 中管理来自未知来源的安装? [英] How to manage installation from Unknown Sources in Android Oreo?

查看:29
本文介绍了如何在 Android Oreo 中管理来自未知来源的安装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android Oreo (8.0) 中,对如何允许安装来自未知来源的应用程序(从用户的角度)以及获得安装它们的许可的过程(从开发人员的角度)进行了一些更改).

由于我发现在开发人员方面特别难以找到所有必要的步骤,因此我认为在这里寻求解决方案并自己回答问题很有用,现在我找到了答案,以供将来参考,他们面临着同样的障碍.

答案将包括以下问题:

  1. 如何检查是否允许我请求安装软件包?
  2. 我必须请求什么确切的许可?
  3. 如何提示用户授予此权限?
  4. 如何提示用户安装指定的 .apk?

(如果我仍然在这里遗漏任何内容,我将不胜感激任何其他答案或评论指出这一点.)

解决方案

对于初学者,您的应用程序需要在 build.gradle 中声明 26(Android Oreo 的 API 级别)或更高的 targetSdkVersion或 AndroidManifest.xml 使所有这些工作.

接下来回答上面的问题:

<块引用>

  1. 如何检查是否允许我请求安装软件包?

您可以使用 getPackageManager().canRequestPackageInstalls() 活动代码中的任何位置.请注意,如果您未声明适当的权限或针对错误的 SDK 版本,则此方法始终返回 false.

<块引用>

  1. 我必须请求什么确切的许可?

您必须声明 Manifest.permission.REQUEST_INSTALL_PACKAGES 在您的 AndroidManifest.xml 中,如下所示:

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

<块引用>

  1. 如何提示用户授予此权限?

您可以使用 Intent ACTION_MANAGE_UNKNOWN_APP_SOURCES:

startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES));

您还可以通过以下方式更直接地将用户发送到您的应用程序的特定设置:

startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:your.application.package")));

<块引用>

  1. 如何提示用户安装指定的 .apk?

一旦你确定你被授予了适当的权限,你就可以提示用户在你的 Activity 代码中的任何地方安装你的 .apk 文件(其中 this 指的是你的 Activity 的 Context代码>),使用:

Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);intent.setDataAndType(FileProvider.getUriForFile(this, "your.application.package.fileprovider", new File("/path/to/your/apk")), "application/vnd.android.package-archive");intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);开始活动(意图);

你也可以添加intent.putExtra(Intent.EXTRA_RETURN_RESULT, true) 并以startActivityForResult(Intent, int)开头,如果你想知道是否安装成功, 被取消或失败.

有关如何正确获取 .apk 文件的 Uri 的信息,请参阅 FileProvider.

In Android Oreo (8.0), several changes where made on how to allow the installation of apps from Unknown Sources (from the user's point of view) and to the process of getting permission to install them (from the developer's point of view).

Since I found it particularly hard to find all the steps necessary on the developer side, I thought it to be useful to ask here for the solution and answer the question myself, now that I found the answers, for future reference to those, who are facing the same obstacles.

The answer will include the following questions:

  1. How to check whether I'm allowed to request a package install?
  2. What exact permission do I have to request?
  3. How can I prompt the user to grant this permission?
  4. How do I prompt the user to install a specified .apk?

(If I still miss anything here, I'd be grateful for any additional answers or comments pointing that out.)

解决方案

For starters, your application needs to declare a targetSdkVersion of 26 (API level of Android Oreo) or higher in your build.gradle or AndroidManifest.xml for all this to work.

Then on to answer the questions above:

  1. How to check whether I'm allowed to request a package install?

You can check this using getPackageManager().canRequestPackageInstalls() anywhere in your Activity's code. Note that this method always returns false, if you don't declare the appropriate permission or target the wrong SDK version.

  1. What exact permission do I have to request?

You have to declare Manifest.permission.REQUEST_INSTALL_PACKAGES in your AndroidManifest.xml, like so:

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

  1. How can I prompt the user to grant this permission?

You can send the user to the appropriate destination, with Intent ACTION_MANAGE_UNKNOWN_APP_SOURCES:

startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES));

You may also send the user more directly to the specific setting for your application, with:

startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:your.application.package")));

  1. How do I prompt the user to install a specified .apk?

Once you made sure you are granted the appropriate permission, you can prompt the user to install your .apk file anywhere in your Activity's code (where this refers to your Activity's Context), using:

Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setDataAndType(FileProvider.getUriForFile(this, "your.application.package.fileprovider", new File("/path/to/your/apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(intent);

You may also add intent.putExtra(Intent.EXTRA_RETURN_RESULT, true) and start with startActivityForResult(Intent, int), if you want to know if the installation succeeded, was cancelled or failed.

For information on how to correctly get your .apk file's Uri, see FileProvider.

这篇关于如何在 Android Oreo 中管理来自未知来源的安装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆