最近学习安卓,想实现一个用代码打开下载的apk的功能。
下载的apk放在了外部储存根目录建的一个文件夹里面,我在真机上测试的,手机是安卓10 api版本是29,但是启动之后提示解析包时出现问题。
尝试了三个apk,两个是unity发布的,还有一个是android studio 发布的,这三个apk单独安装都没问题,排除apk不完整的可能性;unity发布选的api版本是最低的21,按理说也不是api的问题;
另外代码这边也是按照chatgpt写的,代码也很简单,感觉也没问题,我有点懵,希望能有人指导下。
这个是主要逻辑,就这运行这三个方法,我工程下面有个Module包,里面是一些下载的逻辑,只是没有用上。
package com.fch.Android2Unity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_STORAGE_PERMISSION = 1234;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity", "开始执行下载程序!");
//requestStoragePermission2();
/* ProDownLoad pro = new ProDownLoad(MainActivity.this) ;
pro.Install2OpenApk() ;*/
GetAuthority();
}
// 安装权限请求代码
private static final int INSTALL_PERMISSION_REQUEST_CODE = 123;
//安装和启动
private void GetAuthority() {
Log.e("", "开始获取权限");
// 获取安装权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !getPackageManager().canRequestPackageInstalls()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, INSTALL_PERMISSION_REQUEST_CODE);
Log.e("", "没权限");
} else {
// 如果已经有安装权限,则直接安装
Log.e("", "有权限");
installApk();
}
}
// 在Activity中处理权限请求的结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == INSTALL_PERMISSION_REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && getPackageManager().canRequestPackageInstalls()) {
Log.e("", "开始安装");
// 如果用户授予了安装权限,则进行安装
installApk();
} else {
// 如果用户拒绝了安装权限,则提示用户
Log.e("", "用户拒绝了安装权限");
}
}
}
// 安装APK包
private void installApk() {
File file = new File("/storage/emulated/0/CreateParty/Install/DJ_Install.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(this, "com.fch.Android2Unity.fileprovider", file);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}
}
下面是配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE "/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Android2Unity"
tools:targetApi="21">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.fch.Android2Unity.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path name="create_party" path="."/>
</paths>
</resources>
build.gradle 文件:
plugins {
id 'com.android.application'
}
android {
namespace 'com.fch.Android2Unity'
compileSdk 33
defaultConfig {
applicationId "com.fch.Android2Unity"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation project(path: ':LoadLib')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
下面是我的工程目录: