Android N install apk via code

2019-05-23 14:16发布

问题:

I am trying to install an APK on android N

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        File apkFile = new File(Environment.getExternalStorageDirectory() + "/my/myapk.apk");
        Uri apkURI = FileProvider.getUriForFile(mContext,
                BuildConfig.APPLICATION_ID + ".provider",
                apkFile);
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(apkURI);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        mContext.startActivity(intent);
    }

The app is installing but it is not opening the app.

On an SDK below N with the below code it was installing and after installation it would open the app.

 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/my/myapk.apk")), "application/vnd.android.package-archive");
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 mContext.startActivity(intent);

On android N how can I install APK through code and then open once installation is complete?