Skip to content

Latest commit

 

History

History
114 lines (96 loc) · 5.06 KB

README.md

File metadata and controls

114 lines (96 loc) · 5.06 KB

How to install .apk-Files with Java

Here you can learn how to install .apk-Files with Java on Android Devices

Content:

  1. Manifest
  2. Check unknown sources
  3. Starting the Installation

Manifest

For reading the storage you need:

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

To install the .apk-File on API Level 26 and higher allow REQUEST_INSTALL_PACKAGES:

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

Check unknown sources

First we need to check that this App can install 3rd-Party-Apps without the PlayStore. For this we need to check the Settings for this App with:

if(!getPackageManager().canRequestPackageInstalls())

If the option ACTION_MANAGE_UNKNOWN_APP_SOURCES is disabled for this App than we can open the setting for this package for the user with this code:

startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:COM.PACKAGE.NAME")));

Starting the Installation

Note: You need different Intents for different API Levels

Android-Version SDK/API Intent Methode
5.0(Lollipop) - 7.1(Nougat) 21-25 ACTION_VIEW Here
8.0(Oreo) - 9.0(Pie) 26-28 ACTION_INSTALL_PACKAGE Here
10-11 29-30 MODE_FULL_INSTALL Here

for API 21 to 25

It uses the Intent ACTION_VIEW.

First define the Path to the .apk-File:

    //define the path
    path = "storage/emulated/0/folder/app.apk";

Then use the StrictMode:

    //use the StrictMode
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());

After that create and define the Intent with ACTION_VIEW:

    //create and define the Intent
    Intent intent = new Intent(Intent.ACTION_VIEW);

You must also define the File and the Type for the Intent. We use our defined variable path as the data and the type application/vnd.android.package-archive like this:

    //set the path and the type for the .apk-File
    intent.setDataAndType(Uri.fromFile(new java.io.File(path)),"application/vnd.android.package-archive");

Then use FLAG_ACTIVITY_NEW_TASK to define an atomic group of activities that the user can move to:

   //setFlags to hide Error
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

And at the last you can start the installation with this code:

    //startActivity for this intent
    startActivity(intent);

for API 26 to 28

Note: This Methode works only from API 26 to 28 and is deprecated for API 29 and higher. You can read it here.

First define the Path to the .apk-File:

    //define the path
    path = "storage/emulated/0/folder/app.apk";

Then use the StrictMode:

    //use the StrictMode
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());

After that create and define the Intent with ACTION_INSTALL_PACKAGE:

    //create and define the Intent
    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);

You must also define the File for the Intent. We use our defined variable path as the data like this:

    //set the path and the type for the .apk-File
    intent.setDataAndType(Uri.fromFile(new java.io.File(path)));

And at the last you can start the Installation with this code:

    //startActivity for this intent
    startActivity(intent);

for API 29 to 30

Use PackageInstaller(). I havn't found anything about that. 😕