添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
使用FileProvider
1:自定义一个provider,空的即可
public class MyFileProvider extends FileProvider {
}
2:注册此provider
<provider
android:name=".support.update.MyFileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/crms_file_paths" />
</provider>
3:添加provider的xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path
name="external_files"
path="." />
<external-path
name="Download"
path="Download/"/>
<files-path
name="files"
path="." />
<external-cache-path
name="external_cache"
path="." />
</paths>
安装适配
//判读版本是否在7.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//在AndroidManifest中的android:authorities值
String filepath = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+"/"+"crms.apk";
Uri puri = FileProvider.getUriForFile(context,BuildConfig.APPLICATION_ID+".fileprovider", new File(filepath));
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//添加这一句表示对目标应用临时授权该Uri所代表的文件
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(puri, "application/vnd.android.package-archive");
context.startActivity(install);
} else {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(install);
}
暂无评论
要发表评论,您必须先 登录