Android 7.0 以上 安装 apk 的方法

1.安装apk方法

/**

*

* @param context 上下文

* @param url apk安装路径

*/

public static void doInstall(Context context, String url) {

File apkFile = new File(url);

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//判断是否是7.0以上版本

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 {

//7.0以下安装apk

intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");

}

context.startActivity(intent);

}

 

2.在AndroidManifest.xml 文件中添加provider 如下:

 

android:name=".util.AppUpdateFileProvider"

android:authorities="${applicationId}.fileprovider"

android:exported="false"

android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/update_paths" />

 

其中 AppUpdateFileProvider是 util包下的 类 如下:

public class AppUpdateFileProvider extends FileProvider { }

applicationId 是指 build.gradle文件下的包名

xml/update_paths 是指在 res包下创建 xml包 xml包下面创建的文件 是 update_paths.xml

其中内容是

 

有什么疑问的 可以留言 

你可能感兴趣的:(android)