3

I'm developing an app and want to support Android Q. And I'm using the Android Emulator. I have downloaded an apk file in the path.

/storage/emulated/0/Android/data/<package-name>/files/download/xxxxxx.apk

I install the apk like this:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri apkUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", new File(apkPath));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");        
startActivity(intent);

The above code is working fine before Android Q. But on Android Q, I only can get the file uri

content://<package-name>.fileprovider/my_downloads/xxxxxx.apk

, but the installation screen is not shown when sending the intent. I need someone's help. Thanks very much!

Fen Li
  • 209
  • 2
  • 8
  • See the most recent answer for [Install Application programmatically on Android](https://stackoverflow.com/a/54424223/295004) – Morrison Chang May 06 '19 at 03:26
  • please check this one. here's the ling [https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android](https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android)! – Christian Rey Baron May 06 '19 at 03:50

1 Answers1

3

The problem is about installing unknown app after Android O. I should use the permission:

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

and check the permission before installation:

getPackageManager().canRequestPackageInstalls()

If the permission has been granded, it's ok to continue the installation. Otherwise, we should send the intent to open the permission page:

Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_INSTALL_PERMISSION);

And we can handle the result in onActivityResult.

Fen Li
  • 209
  • 2
  • 8
  • Isn't `android.permission.REQUEST_INSTALL_PACKAGES` available to only system apps or apps signed with system keystore or whatever? – pavi2410 Nov 05 '19 at 12:48