I am trying to get my app to automatically install an apk. This works fine for api<24. But for 24, it is failing. Android has implemented extra security:
For Oreo, Add permission in AndroidManifast (Otherwise it just silently fails)
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
now add to you'r Manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
in xml directory add...
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." /></paths>
then use these codes where you want.
File directory = Environment.getExternalStoragePublicDirectory("myapp_folder");
File file = new File(directory, "myapp.apk"); // assume refers to "sdcard/myapp_folder/myapp.apk"
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //dont forget add this line
context.startActivity(intent);
}
For Oreo, Add permission in AndroidManifast
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>