android - 如何在 Android 10 中以编程方式安装任何 Android 应用
问题描述
在 Android 9 和 10 中,我使用文件路径 apk 文件在 Android Studio 中以编程方式安装应用程序。下面显示我尝试过的..
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.parse("content://"+Environment.getExternalStorageDirectory() + "/download/" + "app-release.apk"), "application/vnd.android.package-archive");
startActivity(intent);
我还在清单文件中添加了所需的权限。
当我运行它时,它给了我解析包错误时出现问题。
解决方案
如果您想在 Android 10 中以编程方式安装应用程序,您需要授予为您的应用程序安装应用程序的权限
第 1 步:在 Manifest 中授予权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
第 2 步:在 Android Manifest 中编写提供程序
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
第 4 步:在名为 provider_paths.xml 的 XML 文件夹中创建文件并写入
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="files_root"
path="Android/data/${applicationId}" />
<external-path
name="external_files"
path="." />
</paths>
第4步:授予apk安装和存储权限的运行时权限
//installtion permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!getPackageManager().canRequestPackageInstalls()) {
startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))), 1234);
} else {
}
}
//Storage Permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
第 5 步:调用 install apk 函数
void installAPK(){
String PATH = Environment.getExternalStorageDirectory() + "/" + "apkname.apk";
File file = new File(PATH);
if(file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uriFromFile(getApplicationContext(), new File(PATH)), "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
getApplicationContext().startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Log.e("TAG", "Error in opening the file!");
}
}else{
Toast.makeText(getApplicationContext(),"installing",Toast.LENGTH_LONG).show();
}
}
Uri uriFromFile(Context context, File file) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
} else {
return Uri.fromFile(file);
}
}
推荐阅读
- javascript - 如何获取上一个日期?
- django - Django 信号 post_save 未创建返回两次
- c# - C# 窗体关闭并重新打开,抛出“无法访问已处理的对象”
- angular - 使用带有阴影 dom 的角度元素的角度材料
- python - 我只是无法让 displayMousePosition 工作-python
- markdown - 如何在 md 文件中显示此 pypi 下载?
- css - 如何在悬停时在多个帧之间进行图像循环?
- python - 在 Django 中为给定日期添加天数
- python - 使用 shutil 创建 ZIP 文件夹 - Python
- c# - OpenTK - 片段着色器不工作(三角形总是黑色)