80,351
社区成员
发帖
与我相关
我的任务
分享
private static void installNormal(Context context, String apkPath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
// 版本在7.0以上是不能直接通过uri访问的
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
File file = (new File(apkPath));
// 由于没有在Activity环境下启动Activity,设置下面的标签
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件
Uri apkUri = FileProvider.getUriForFile(context, "com.my.localopera.provider", file);
// 添加这一句表示对目标应用临时授权该Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
}
context.startActivity(intent);
}
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path path="Android/data/com.my.localopera/" name="files_root"/>
<external-path path="myDownload/" name="root"/>
</paths>
</resources>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.my.localopera.provider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path" />
</provider>
String path = getStoragePath(this, false);
String localPath = path + "/meigongmeijiang";
Log.i("localPath", localPath);
File filenew = new File(localPath,"test2.apk");
// if (filenew.exists()) {
// Log.i("file", "file is exist");
installApk(this,filenew.getAbsolutePath());
// }
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<root-path name="root" path=""/>
</paths>
</resources>