SoFunction
Updated on 2025-04-05

Android installation apk file and adapt to Android 7.0 details

Android installation apk file and adapt to Android 7.0 details

First register the provider in the file, activity node at the same level:

<provider
      android:name="."
      android:authorities="${applicationId}.file_provider"
      android:exported="false"
      android:grantUriPermissions="true">
      <meta-data
        android:name=".FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
    </provider>

Download the apk file to this path:

String cachePath = (
            getExternalFilesDir("upgrade_apk") +
                 +
                getPackageName() +
                ".apk");

Create a file named file_paths in the res directory xml folder: upgrade_apk represents the folder name of the saved path above, you can change it at will, the same is enough.

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-files-path name="bga_upgrade_apk" path="upgrade_apk" />
</paths>

Finally, write the code to distinguish the version numbers of different Android systems and install the apk (note: [] you need to replace the package name of your application)

 /**
        * Install the apk file
        *
        * @param apkFile
        */
      public void installApk(File apkFile) {
        Intent installApkIntent = new Intent();
        (Intent.ACTION_VIEW);
        (Intent.CATEGORY_DEFAULT);
        (Intent.FLAG_ACTIVITY_NEW_TASK);

        if (.SDK_INT &gt; Build.VERSION_CODES.M) {
          ((getApplicationContext(), ".file_provider", apkFile), "application/-archive");
          (Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
          ((apkFile), "application/-archive");
        }

        if (getPackageManager().queryIntentActivities(installApkIntent, 0).size() &gt; 0) {
          startActivity(installApkIntent);
        }
      }

Thank you for reading, I hope it can help you. Thank you for your support for this site!