Android N安装apk通过代码

时间:2017-01-20 11:00:30

标签: android android-7.0-nougat

我正在尝试在Android N上安装APK

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);
    }

应用正在安装,但它没有打开应用。

在N下面的SDK上安装了以下代码,安装后会打开应用程序。

 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);

在Android N上如何通过代码安装APK,然后在安装完成后打开?

0 个答案:

没有答案