1. 程式人生 > >android程式碼呼叫安裝apk(相容7.0)

android程式碼呼叫安裝apk(相容7.0)

 public void install(Context context,String filePath) {
        File apkFile = new File(filePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

            intent.setFlags
(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile( context , "你的包名.fileprovider" , apkFile); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { intent.setDataAndType
(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); } context.startActivity(intent); }

相容7.0

1.在配置檔案中新增

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="你的包名.fileprovider"
            android:exported
="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>

2.在res中新增檔案file_paths.xml檔案
這裡寫圖片描述

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path
        name="root_path"
        path="." />
</paths>