我已经设置了Broadcast
当 Application
时启动安装有startActivityForResult()
。安装Intent
在这里创建。
private static Intent getOpenDownloadedApkIntent(Context context, File file) {
String name = getPackageNameForAPK(file.getPath(), context);
if (name != null) {
INSTALL_APK_INFO.put(name, file);
}
// The type of intent to use to open the downloaded apk changed in Android N (7.0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri path = FileProvider.getUriForFile(context,
context.getApplicationContext().getPackageName() + ".utils.DownloadedFileProvider",
file);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
intent.setData(path);
return intent;
} else {
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
return intent;
}
}
private static String getPackageNameForAPK(final String archiveFilePath, Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageArchiveInfo(archiveFilePath, PackageManager.GET_META_DATA);
return info.packageName;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
现在,如果是Android 7或更高版本,则可以卸载,但如果是Android 6或更低版本,则似乎只是删除APK
的图标。但不是APK
本身。
这是应该删除 APK
的代码:
private static BroadcastReceiver onInstallComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ApplicationInfo info = null;
try {
info = context.getPackageManager().getApplicationInfo(intent.getDataString().split(":")[1], PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException ex) {
}
try {
if (info != null) {
File file = INSTALL_APK_INFO.get(info.packageName);
if (file != null) {
file.delete();
INSTALL_APK_INFO.remove(info.packageName);
}
}
} catch (NullPointerException ex) {
}
}
};
我猜测这与路径有关,但同时它似乎删除了APK
的图标。我所说的图标是指如果我删除 file.delete()
然后APK
下载文件夹中带有图标,但如果我运行 file.delete()
然后APK
没有图标。
我做错了什么?
最佳答案
您的意思是说应用程序正在安装但没有启动器图标?
关于java - APK 未从下载文件夹中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57605196/