项目有安装应用的需求,但发现网上的安装,有的在Android版本高一些,或者特定机型不适配,我记录下。
public void installApk(Context context,String downloadApk) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(context,"com.vone.packApk.packguoshu",new File(downloadApk));
intent.setDataAndType(uri,"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
intent.setDataAndType(Uri.parse("file://" + downloadApk),"application/vnd.android.package-archive");
}
context.startActivity(intent);
}