问题描述
我尝试制作一个可以控制设备上的应用程序为最新的应用程序,并在需要时进行更新/安装。
为了进行开发,我将设备与 android 9版一起使用,并且在其中一切正常,都使用了文件提供程序。但是我需要它在具有 android 5的设备上运行,并且出现“这里是解析软件包时出现的问题”错误。
我尝试了在SO和其他论坛上推荐的所有方法,但似乎无济于事。
我尝试添加各种清单权限,意图标志,将apkFile.setReadable(true,false)
添加为某人推荐的内容,等等。
这是我的代码:
File apkPath = new File(getApplicationContext().getFilesDir(),"apks");
if (!apkPath.exists()) {
if (!apkPath.mkdirs()) {
throw new FileErrorException(apkPath + " mkdir fail");
}
}
File apkFile = new File(apkPath,"app" + ".apk");
if (apkFile.exists()) {
if (!apkFile.delete()) {
throw new FileErrorException(apkFile + " file delete fail");
}
}
FileOutputStream stream = new FileOutputStream(apkFile);
stream.write(myApkByteArray);
stream.close();
Intent install;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //this part works fine on the device with android 9
Uri apkUri = FileProvider.getUriForFile( context,BuildConfig.APPLICATION_ID + ".fileprovider",apkFile);
install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.setData(apkUri);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else { //but this part does not work for Android 5
Uri apkUri = Uri.fromFile(apkFile);
install = new Intent(Intent.ACTION_VIEW,apkUri);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
install.setDataAndType(apkUri,"application/vnd.android.package-archive");
}
activity.startActivity(install);
清单部分:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="myApp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<Meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
文件提供者的XML:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="apk" path="apks/"/>
</paths>
logcat中没有错误或警告。
任何帮助将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)