简体   繁体   中英

How to install an external apk via Intent

i am trying to install an apk from my app. The app i want to intall is another version of the current app. So it is basically an update function. This is the code i am using:

static final String FILE_BASE_PATH = "file://";
static final String PROVIDER_PATH = "de.company.name.provider";
static final String APP_INSTALL_PATH = "\"application/vnd.android.package-archive\"";

String path = Environment.getExternalStorageDirectory() + "/DirName/" + "AppName.apk";    
Uri uri = null;
Intent installIntent = new Intent();
installIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   uri = FileProvider.getUriForFile(context, PROVIDER_PATH, new File(path)); 
   installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
   installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   installIntent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
   installIntent.setData(uri);
} else {
   uri = Uri.parse(FILE_BASE_PATH + path);
   installIntent.setDataAndType(uri, APP_INSTALL_PATH);
}
context.startActivity(installIntent);

These are my permissions in AndroidManifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES " />

This is my provider declaration in AndroidManifest:

<provider
   android:name="androidx.core.content.FileProvider"
   android:authorities="de.company.name.provider"
   android:exported="false"
   android:grantUriPermissions="true">
   <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/provider_paths" />
</provider>

And these are my provider_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="dir" path="DirName" />
    <external-path name="external_dir" path="DirName" />
</paths>

My sdk version is 32. After executing the code a menu of apps to execute shows up. When i choose "Package Installer" it starts for 1sec and disappears then without installing my app. When i simply click on the downloaded apk icon the installation starts How do i make this work from my code? Thanks for your help

  • Morris

Got it Mistake was a single space in the permission REQUEST_INSTALL_PACKAGE. So i changed

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

to

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

now it works fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM