如何在Android 10中以编程方式安装任何Android应用 [英] How to install any Android app programmatically in Android 10
问题描述
在Android 9及更高版本中10 我面对在Android Studio中使用文件路径apk文件以编程方式在安装应用程序中发布的问题.下面显示了我尝试过的方法.
In Android 9 & 10 I face issued in install app programmatically in Android Studio using filepath apk file. Below show what I tried..
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);
我还在清单文件中添加了必需的权限.
I also added required permission in manifest file .
运行此程序时,它会给我解析程序包时出现问题.
When I run this then it give me There was a problem parsing the package error.
推荐答案
如果要在Android 10中以编程方式安装应用程序,则需要授予权限才能为您的应用程序安装应用程序
If you want install application programmatically in Android 10, You need to give permission to install app for your application
步骤1:在清单中授予权限
Steps 1: Give permission in 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中编写提供程序
Step 2: Write provider in 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文件夹中创建文件并写入
Step 4: Create file in XML folder named as provider_paths.xml and write
<?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安装和存储权限授予运行时权限
step 4: Give runtime permission for apk installation and storage permissions
//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函数
Step 5: Call install apk function
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);
}
}
这篇关于如何在Android 10中以编程方式安装任何Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!