Android N install apk via code

落爺英雄遲暮 提交于 2019-12-10 11:17:00

问题


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?

来源:https://stackoverflow.com/questions/41762043/android-n-install-apk-via-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!