Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

security - How to manage installation from Unknown Sources in Android Oreo?

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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...