简体   繁体   中英

Trying to install apk with intent fails without error

I'm trying to install an apk after I downloaded it.
Download works fine but when I try to install the package it does absolutely nothing.
There are no log messages and I have no clue what the my error might be.

Here is the code I use to launch the intent (it definitely gets executed):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Uri contentUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", apkFile);
    Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    install.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
    install.addCategory(Intent.CATEGORY_DEFAULT);
    install.setDataAndType(contentUri, "application/vnd.android.package-archive");
    context.startActivity(install);
} else {
    Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
    context.startActivity(install);
}

I've checked the variables:

  • apkFile: "/storage/emulated/0/Download/name.apk"
  • contentUri: "content://APP_ID.provider/external_storage_root/Download/name.apk"

Any tips on what I could try are greatly appreciated.

Nevermind I made a typo in my AndroidManifest.xml.

How it was:

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

How it should be:

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

Notice that one space at the end?
Took me 3 days to find it...

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